//基本数据类型
int i = 10;
float f = 2.12f;
double d = 3.123e+11;
char c = 'x';
NSLog(@"i:%d", i);
NSLog(@"f:%.2f", f); //float类型保留2位小数
NSLog(@"d:%e", d);
NSLog(@"c:%c", c);
//打印x对应的 ASCII值
NSLog(@"ASCII c = %d", c); //120
//打印基本数据类型所占用的字节
NSLog(@"sizeOf i = %ld", sizeof(i));
NSLog(@"sizeOf f = %ld", sizeof(f));
NSLog(@"sizeOf d = %ld", sizeof(d));
NSLog(@"sizeOf c = %ld", sizeof(c));
//基本数据类型的转换
int i1 = 123;
int i2 = 456;
double d1 = (i1 + i2) * 1.5;
NSLog(@"d1:%.2f", d1);
//强制类型转换
float f1 = (float)((i1 + i2) * 1.5 ); //在前面加上小括号,然后写上数据类型就是强制转换
NSLog(@"f1:%f", f1);
Byte b1 = 56; //直接把int类型的值赋值给Byte类型,是可以的,只要不超过Byte类型的范围 Byte: 127
Byte b2 = 77;
Byte b3 = (Byte)(b1 + b2);
NSLog(@"b3:%d", b3);
double d2 = 1e50;
float f2 = (float)d2; //转换出问题了,会产生溢出,因为float类型的数值范围最大是1e38
NSLog(@"f2:%f", f2); //inf : 无穷大的值
float f3 = 1.23f;
long l1 = 123;
long l2 = 6542343243200L;
float f4 = l1 + l2 + f3; //系统会将它转换成float类型计算的
long l3 = (long)f4;
NSLog(@"l3:li", l3); //强制转换的时候会舍去小数部分
NSLog(@"f4:%f", f4);
- ( void) getNumber
{
int i1 = 10, i2 = 12;
int i = i2 ++;
NSLog(@"i = %d", i);
NSLog(@"i2 = %d", i2);
i = ++i2;
NSLog(@"i = %d", i);
NSLog(@"i2 = %d", i2);
i = --i1;
NSLog(@"i = %d", i);
NSLog(@"i2 = %d", i1);
i = i1--;
NSLog(@"i = %d", i);
NSLog(@"i2 = %d", i1);
}