类型 存储长度 值范围
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
为了得到确切的大小,类型或变量在特定平台上,可以使用sizeof运算符。表达式sizeof(类型)产生的对象或类型以字节为单位的存储大小。以下是这个例子中的任何一台机器上的 int类型的大小:
#import <Foundation/Foundation.h>
int main()
{
NSLog(@"Storage size for int : %d
", sizeof(int));
return 0;
}
当编译并执行上述程序,在Linux上产生以下结果:
2013-09-07 22:21:39.155 demo[1340] Storage size for int : 4
浮点类型
下表给出了有关标准的存储大小和取值范围和精度的浮点类型的详细信息:
类型 存储大小 取值范围 精确
float 4 byte 1.2E-38 to 3.4E+38 6 decimal places
double 8 byte 2.3E-308 to 1.7E+308 15 decimal places
long double 10 byte 3.4E-4932 to 1.1E+4932 19 decimal places
头文件float.h中定义的宏,允许使用这些值和其他详细信息在程序中实数的二进制表示。下面的例子将打印存储空间所采取的浮点类型,其范围值:
#import <Foundation/Foundation.h>
int main()
{
NSLog(@"Storage size for float : %d
", sizeof(float));