EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Objective C

Variables and Data Types Objective C

Computer Science / Objective C tutorial chapter - Published 2025-12-16 - 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:

Variables and Data Types Objective C
TypeDescription
Primitive Data TypesArithmetic types, further classified into integer and floating-point data types.
Void TypesData types with no value or operator, used when functions do not return a value.
Derived TypesIncludes pointers, arrays, structures, unions, and function types.
Enumerated TypesArithmetic 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:

Variables and Data Types Objective C
TypeStorage SizeFormat SpecifierExamples
char1 byte%c'a''\n'
short int2 bytes%hi%hx%ho
unsigned short int2 bytes%hu%hx%ho
int2 or 4 bytes%i%x%o12-970xFFE0
unsigned int2 or 4 bytes%u%x%o12u100U
long int8 bytes%li%lx%lo12L-2001
unsigned long int8 bytes%lu%lx%lo12UL0xffeeUL
long long int8 bytes%lli%llx%llo0xe5e5e5e5LL
unsigned long long int8 bytes%llu%llx%llo12ull0xffeeULL
float4 bytes%f%e%g%a12.34f3.1e-5f
double8 bytes%f%e%g%a12.343.1e-5
long double10 bytes%Lf%Le%Lg12.34L3.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 <Foundation/Foundation.h>
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 <Foundation/Foundation.h>
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 <Foundation/Foundation.h>
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 <Foundation/Foundation.h>
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 <Foundation/Foundation.h>
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 <Foundation/Foundation.h>
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 <Foundation/Foundation.h>
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 <Foundation/Foundation.h>
int main() {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *name = @"Objective-C";
    NSLog(@"Programming Language: %@", name);

    [pool drain];
    return 0;
}

Output:

Programming Language: Objective-C
End of lesson.