Open In App

Data Types in Objective-C

Last Updated : 03 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what kind of data is going to store in the variable and how much space it occupies in storage. The data types in Objective-C can be classified as follows:

Types Description
Primitive Data Types Arithmetic types can be further classified into integer and floating data types.
Void Types The data type has no value or operator and it does not provide a result to its caller.
Derived Types It includes pointer types, array types, structure types, union types, and function types
Enumerated Types They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.
Data-types-in-objective-C

 

Data types also have different types of ranges according to the compiler in Objective-C. Below is the list of data types in Objective-C. 

Type Storage size NSLog Chars Constant Examples
char 1 byte %c ‘a’, ‘\n’
short int 2 byte %hi, %hx, %ho
unsigned short int 2 byte %hu, %hx, %ho
int 2 or 4 byte %i, %x, %o 12, -97, 0xFFE0, 0177
unsigned int 2 or 4 byte %u, %x, %o 12u, 100U, 0XFFu
long int 8 byte %li, %lx, %lo 12L, -2001, 0xffffL
unsigned long int 8 byte %lu, %lx, %lo 12UL, 100ul, 0xffeeUL
long long int 8 byte %lli, %llx, &llo 0xe5e5e5e5LL, 500ll
unsigned long long int 8 byte %llu, %llx, %llo 12ull, 0xffeeULL
float 4 byte %f, %e, %g, %a 12.34f, 3.1e-5f, 0x1.5p10, 0x1P-1
double 8 byte %f, %e, %g, %a 12.34, 3.1e-5, 0x.1p3
long double 10 byte %Lf, $Le, %Lg 12.34L, 3.1e-5l

Void

In Objective C, the void type represents the absence of a value. Instead of typing the function variable name, you can define the function by the void that tells us, the function does not return any value to the caller.

-(void) function-name;

Nil and Null

These nil and Null keywords represent the empty pointer in the memory. This is useful in some cases when you declare a pointer but that time this does not point to the other variable address that time so you initialize the by nil or null keyword.

Integers Type

Objective-C supports integer data type. This data type stores the whole number, hexadecimal values, and octal values. We can determine the size of the integer type using sizeof the operator in Objective-C. Unsigned int data type in Objective-C is used for storing 0 to positive numbers but it can’t store the negative number if you want to store the negative number you can use the int which can store negative numbers as well as positive numbers. 

  • Size: 2 or 4 byte
  • Ranges: 2,147,483,648 to 2,147,483,647

Syntax:

int a =  19;

Example:

ObjectiveC




// Objective-C program for Integer Data Type
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    // Integer value with positive data
    int a =  9;
     
    // Integer value with negative data
    int b = -9;
     
    // Unsigned int
    int c = 89U;
     
    // Long int
    long int d = 9998L;
     
    NSLog(@"Integer value with positive data = %x\n", a);
    NSLog(@"Integer value with negative data = %d\n", b);
    NSLog(@"Integer value with an unsigned int data  = %u\n", c);
    NSLog(@"Integer value with an long int data: = %li\n", d);
     
    [pool drain];
    return 0;
}


Output:

Integer value with positive data = 9
Integer value with negative data = -9
Integer value with an unsigned int data  = 89
Integer value with an long int data: = 9998

Short Integers Type

Objective-C also has a short integer that can store a 2-byte integer value in the computer memory. A Short integer is a signed type. It can also store positive values and negative values.

  • Size: 2 byte
  • Range: -32768 to 32767

Syntax:

short int number = 67;

Example:

ObjectiveC




// Objective-C program to demonstrate the short integer
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     
    // Short integer
    short int number = 20;
     
    printf(" Short Intger Value  = %d", number);
    [pool drain];
    return 0;
}


Output:

Short Intger Value  =  20

Long Integers Type

If the int data type does not fulfill your needs. Then you can shift to the long int that can hold 8 bytes of data in the modern computer system. This is large enough to hold any numbers. It is a signed type so you can store positive as well as negative numbers.

  • Size: 8 bytes
  • Range: -9223372036854775808 to 9223372036854775807

Syntax:

long int bigInt = 9223372036854775807;

Example:

ObjectiveC




// Objective-C program to demonstrate the long integer
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     
    // Long int
    long int bigInt = 9223372036854775807;
     
    NSLog(@"The big integer is: %li", bigInt);
     
    [pool drain];
    return 0;
}


Output:

The big integer is: 9223372036854775807

Character Type

Character data type allows its variable to store only a single character. The storage of character data type is 1 byte almost in all compilers. It is the most basic data type in Objective-C.

  • Range: (-128 to 127) or (0 to 255)
  • Size: 1 byte

Syntax:

char a = ‘v’;

Example:

ObjectiveC




// Objective-C program to illustrate the character data type
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    // Character data type
    char a = 'a';
    char c;
    NSLog(@"Value of a: %c\n", a);
    a++;
     
    NSLog(@"Value of a after increment is:  %c\n", a);
     
    // c is assigned ASCII values
    // which corresponds to the
    // character 'c'
    c = 99;
    NSLog(@"Value of c: %c\n", c);
     
    [pool drain];
    return 0;
}


Output:

Value of a:   a
Value of a after increment is:  b
Value of c: c

Floating Point Types

Floating Point data types store the floating point values in Objective-C. Float in Objective-C is used to store decimal and exponential values. It is used to store decimal numbers(numbers with floating values) with single precision.

  • Range: 1.2E-38 to 3.4E+38
  • Size: 4 bytes

Syntax:

float a = 9.8f;

Example:

ObjectiveC




// Objective-C program to illustrate the floating data types
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    // Floating point data type
    float a = 9.0f;
    float b = 2.5f;
    float c = 2E-4f;
     
    NSLog(@"%f\n", a);
     
    NSLog(@"%f\n", b);
    NSLog(@"%f", c);
     
    [pool drain];   
    return 0;
}


Output:

9.000000
2.500000
0.000200

Double Types

A double data type in Objective-C is used for storing decimal numbers(numbers with floating values) with double precision. It is used for storing numerical values which hold the decimal point in Objective-C. It is called the double data type because it can hold the double size of data as compared to the float type.

  • Range:  2.3E-308 to 1.7E+308
  • Size: 8 bytes

Syntax:

double b = 12.293123;

Example:

ObjectiveC




// Objective-C program to illustrate the double data types
#import <Foundation/Foundation.h>
int main()
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    // Here we are assigning double data types
    double a = 123123123.00;
    double b = 12.293123;
    double c = 2312312312.123123;
    
    NSLog(@"%lf\n", a);
    NSLog(@"%lf\n", b);
    NSLog(@"%lf", c);
     
    [pool drain];   
    return 0;
}


Output:

123123123.000000
12.293123
2312312312.123123

Boolean Type

In Objective C, we can also store the boolean values in the Bool type. Objective-C also defines the true or false keywords, which are YES and No respectively. 

Syntax:

bool btm  = false;

Example:

ObjectiveC




// Objective-C program to illustrate the bool data types
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     
    // Creating boolean data type
    bool isHuman  = true;
     
    if (isHuman == true)
    {
        printf("He is a Human");       
    }else
    {
        printf("He is not Human");
    }
     
    [pool drain];
    return 0;
}


Output:

He is a human

String Data Type

The string is an object in Objective C. We can create a string by using the NSString class and its subclass NSMutableString provides several ways of creating the string object in Objective-C.

Syntax:

NSString *ob = ‘GeeksforGeeks’ ;

Example:

ObjectiveC




// Objective-C program to illustrate the String Data Type
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     
    // Defining the String Data Type
    NSString *name = @"GeeksforGeeks";
     
    NSLog(@"String example = %@\n", name );
     
    [pool drain];
    return 0;
}


Output:

String example = GeeksforGeek

User Defined Data Types

User-defined types are those types that are built by the user using existing data types like int, short int, etc. In Objective C, We can define user-defined data types using structure. To define a structure, you must use the struct keyword that makes the structure. The struct makes the new data type that can store many types in a single type 

Syntax:

struct [structure tag] {
  member definition;
  member definition;
  …
  member definition;
} [one or more structure variables];  

The structure tag is optional and member definitions such as int, char, or any other valid data types. Before the semicolon, structure variables are defined that are used for accessing the structure member. See the below example.

ObjectiveC




// Objective-C program to illustrate the User Defined Data Types
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     
    // Here, We are defining the Structure
    struct empl
    {
        int emplId;
        int empSalary;
    }empOb;
     
    // Here, we assigning the  values in the structure     
    empOb.emplId = 23;
    empOb.empSalary = 5000;
     
    NSLog(@"Employe Id  = %i", empOb.emplId);
    NSLog(@"Salary = %i ", empOb.empSalary);
     
    [pool drain];
    return 0;
}


Output:

Employe Id  = 23
Salary = 5000 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads