请看一下这段代码:
#include <stdio.h>
int main(void)
{
short s = -1;
printf("sizeof(short) = %lu\n", sizeof(short));
printf("sizeof(int) = %lu\n", sizeof(int));
printf("sizeof(long) = %lu\n", sizeof(long));
printf("s = %hd\n", s);
printf("s = %d\n", s);
printf("s = %ld\n", s);
return 0;
}
它给出的输出为:
sizeof(short) = 2
sizeof(int) = 4
sizeof(long) = 8
s = -1
s = -1
s = 4294967295
在最后一行中,为什么s = 4294967295
不是s = -1
通过这个问题,我才知道在 C 中,当变量被提升时,它的值保持不变。