本文共 1433 字,大约阅读时间需要 4 分钟。
#includestruct TS{ int i; int j;};struct TS ts;int main(){ short s = 0x1122; char c = (char)s; // 0x22 int i = (int)s; // 0x00001122 int j = (int)3.1415; // 3 unsigned int p = (unsigned int)&ts; //long l = (long)ts; // error //ts = (struct TS)l; // error printf("s = %x\n", s); printf("c = %x\n", c); printf("i = %x\n", i); printf("j = %x\n", j); printf("p = %x\n", p); printf("&ts = %p\n", &ts); return 0;}输出结果:s = 1122c = 22i = 1122j = 3p = 804a024&ts = 0x804a024
注意
隐式类型转换的发生点
#includeint main(){ char c = 'a'; int i = c; // safe unsigned int j = 0x11223344; short s = j; // unsafe printf("c = %c\n", c); printf("i = %d\n", i); printf("j = %x\n", j); printf("s = %x\n", s); printf("sizeof(c + s) = %d\n", sizeof(c + s)); return 0;}输出结果:c = ai = 97j = 11223344s = 3344sizeof(c + s) = 4
char和short类型进行计算时会先转换到int类型!