Sat. Apr 20th, 2024

Objective-C: Data types

In objective-c data types are:

  • basic types;
  • enumerated types;
  • type void;
  • derived types;

Integer types:

  • char: 1 byte (-128 to 127)
  • unsigned char: 1 byte (0 to 250)
  • signed char: 1 byte (-128 to 127)
  • int: 2 or 4 bytes (-32768 to 32767)
  • unsigned int: 2 or 4 bytes (0 to 65535)
  • short: 2 bytes (-32768 to 32767)
  • unsigned short: 2 bytes (0 to 65535)
  • long: 4 bytes (-2147483648 to 2147483647)
  • unsigned long: 4 bytes (0 to 4294967295)

To get size of type data or variable you can use operator sizeof. Example:

#import <Foundation/Foundation.h>

int main(){

NSLog(@”Size of int is: %d\n”, sizeof(int));

return 0;

}

Result of above code is: “Size of int is: 4”.

Floating type:

  • float: 4 byte (1.2E-38 to 3.4E+38)
  • double: 8 byte (2.3E-308 to 1.7E+308)
  • long double: 10 byte (3.4E-4932 to 1.1E+4932)

#import <Foundation/Foundatio.h>

int main()

{

NSLog(@”Size of float is: %d\n”, sizeof(float));

}

The void type specify a no value. It’s used for function returns a void or argument for a function( int rand(void)).

Example of void:

#import <Foundation/Foundation.h>

int main(void){

return 0;

}

void rand(){

}

276,919 total views, 1 views today