Contents
Variables and Data Types
Data Types in Objective-C
In Objective-C, each variable is associated with a data type, and each data type requires a specific amount of memory in the system. A data type defines the kind of data a variable will store and the space it occupies in memory. Data types in Objective-C are classified as follows:
Type | Description |
---|---|
Primitive Data Types | Arithmetic types, further classified into integer and floating-point data types. |
Void Types | Data types with no value or operator, used when functions do not return a value. |
Derived Types | Includes pointers, arrays, structures, unions, and function types. |
Enumerated Types | Arithmetic types used to define variables restricted to certain discrete integer values. |
Data Types and Their Properties
Below is the list of common data types in Objective-C along with their storage sizes, format specifiers, and example constants:
Type | Storage Size | Format Specifier | Examples |
char | 1 byte | %c | 'a' , '\n' |
short int | 2 bytes | %hi , %hx , %ho | – |
unsigned short int | 2 bytes | %hu , %hx , %ho | – |
int | 2 or 4 bytes | %i , %x , %o | 12 , -97 , 0xFFE0 |
unsigned int | 2 or 4 bytes | %u , %x , %o | 12u , 100U |
long int | 8 bytes | %li , %lx , %lo | 12L , -2001 |
unsigned long int | 8 bytes | %lu , %lx , %lo | 12UL , 0xffeeUL |
long long int | 8 bytes | %lli , %llx , %llo | 0xe5e5e5e5LL |
unsigned long long int | 8 bytes | %llu , %llx , %llo | 12ull , 0xffeeULL |
float | 4 bytes | %f , %e , %g , %a | 12.34f , 3.1e-5f |
double | 8 bytes | %f , %e , %g , %a | 12.34 , 3.1e-5 |
long double | 10 bytes | %Lf , %Le , %Lg | 12.34L , 3.1e-5l |
Primitive Data Types Examples
1. Integers: The int
type is used to store whole numbers, including decimal, hexadecimal, and octal values. Unsigned integers store only positive values.
Example:
#import
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int a = 9; // Positive integer
int b = -9; // Negative integer
unsigned int c = 89U; // Unsigned integer
long int d = 9998L; // Long integer
NSLog(@"Positive Integer: %i", a);
NSLog(@"Negative Integer: %d", b);
NSLog(@"Unsigned Integer: %u", c);
NSLog(@"Long Integer: %li", d);
[pool drain];
return 0;
}
Output:
Positive Integer: 9
Negative Integer: -9
Unsigned Integer: 89
Long Integer: 9998
2. Short Integers: The short int
type is used for storing 2-byte integer values, which can be positive or negative.
Example:
#import
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
short int number = 67;
NSLog(@"Short Integer Value: %hi", number);
[pool drain];
return 0;
}
Output:
Short Integer Value: 67
3. Long Integers: The long int
type is used when standard int
is insufficient for large values.
Example:
#import
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
long int bigInt = 9223372036854775807;
NSLog(@"Long Integer Value: %li", bigInt);
[pool drain];
return 0;
}
Output:
Long Integer Value: 9223372036854775807
4. Character Data Type: Stores single characters, typically 1 byte.
Example:
#import
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
char a = 'a';
NSLog(@"Character Value: %c", a);
a++;
NSLog(@"After Increment: %c", a);
[pool drain];
return 0;
}
Output:
Character Value: a
After Increment: b
5. Floating Point Types: Floating-point types store decimal numbers.
Example:
#import
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float a = 9.8f;
float b = 2.5f;
NSLog(@"Float A: %f", a);
NSLog(@"Float B: %f", b);
[pool drain];
return 0;
}
Output:
Float A: 9.800000
Float B: 2.500000
6. Double Types: Double precision floating-point values.
Example:
#import
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
double a = 12345.678;
NSLog(@"Double Value: %lf", a);
[pool drain];
return 0;
}
Output:
Double Value: 12345.678000
7. Boolean Type: Used to store true
or false
values, represented as YES
or NO
.
Example:
#import
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL isStudent = YES;
if (isStudent) {
NSLog(@"The person is a student.");
} else {
NSLog(@"The person is not a student.");
}
[pool drain];
return 0;
}
Output:
The person is a student.
8. String Data Type: Strings are objects created using the NSString
class.
Example:
#import
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *name = @"Objective-C";
NSLog(@"Programming Language: %@", name);
[pool drain];
return 0;
}
Output:
Programming Language: Objective-C
Variables in Objective-C
A variable is a name given to a storage area that programs can manipulate. Each variable in Objective-C has a specific type that determines the size and layout of the variable’s memory, the range of values that can be stored, and the operations that can be applied.
The name of a variable can be composed of letters, digits, and underscores. It must begin with a letter or an underscore. Uppercase and lowercase letters are distinct because Objective-C is case-sensitive. The following are the basic variable types in Objective-C:
Sr. No. | Type | Description |
---|---|---|
1 | char | Typically a single octet (one byte). This is an integer type. |
2 | int | The most natural size of integer for the machine. |
3 | float | A single-precision floating-point value. |
4 | double | A double-precision floating-point value. |
5 | void | Represents the absence of type. |
Objective-C also allows various other types of variables, such as enumerations, pointers, arrays, structures, and unions, which are covered in later chapters. This section focuses on basic variable types.
Variable Definition in Objective-C
A variable definition tells the compiler where and how much storage to allocate for the variable. A definition specifies the data type and a list of one or more variables of that type as follows:
type variable_list;
Here, type
must be a valid Objective-C data type, such as char
, int
, float
, double
, BOOL
, or any user-defined object, and variable_list
may consist of one or more identifier names separated by commas. Examples include:
int i, j, k;
char c, ch;
float f, salary;
double d;
Variables can be initialized during their declaration using the following syntax:
type variable_list;
Here, type
must be a valid Objective-C data type, such as char
, int
, float
, double
, BOOL
, or any user-defined object, and variable_list
may consist of one or more identifier names separated by commas. Examples include:
int i, j, k;
char c, ch;
float f, salary;
double d;
Variables can be initialized during their declaration using the following syntax:
type variable_name = value;
Examples:
int a = 3, b = 5; // Definition and initialization
char x = 'x'; // The variable x has the value 'x'
float pi = 3.14; // Definition and initialization
For variables without an initializer:
- Variables with static storage duration are initialized to NULL (all bytes have the value 0).
- The initial value of all other variables is undefined.
Variable Declaration in Objective-C
A variable declaration informs the compiler of a variable’s type and name, enabling the compiler to proceed with compilation without requiring the variable’s complete details. Declarations are particularly useful when working with multiple files. For instance, you can declare a variable in one file and define it in another using the extern
keyword.
extern int a, b;
extern float pi;
Though a variable can be declared multiple times, it can be defined only once in a file, function, or block of code.
Examples:
#import
// Variable declaration
extern int a, b;
extern int c;
extern float f;
int main () {
/* Variable definition */
int a, b;
int c;
float f;
/* Variable initialization */
a = 10;
b = 20;
c = a + b;
NSLog(@"Value of c: %d", c);
f = 70.0 / 3.0;
NSLog(@"Value of f: %f", f);
return 0;
}
Output:
Value of c: 30
Value of f: 23.333334
Lvalues and Rvalues in Objective-C
Expressions in Objective-C can be categorized as:
- Lvalue: Refers to a memory location and can appear on the left or right side of an assignment.
- Rvalue: Refers to a data value stored at an address and can appear only on the right side of an assignment.
Example of Valid assignment:
int g = 20;
Example of Invalid assignment:
10 = 20; // Compile-time error