关于short int 的取值范围
#include<stdio.h>int main()
{
short int b=32768;
short int a = 32767;
printf("%d,%d,%d\n", a, a+ 1, a+ 2);
printf("%d,%d,%d\n", b, b+ 1, b+ 2);
}
结果却是 32767 32768 32769
-32768 -32767 -32766
为什么会出现这种情况
#include<stdio.h> int main() { short int a = 0x7FFF; short int b = 0x8000; printf("%d\n", a); printf("%d\n", b); printf("%d\n", 0x8000); printf("%d\n", 0x7FFFFFFF); printf("%d\n", 0x80000000); }
#include<stdio.h> int main() { printf("16bit 0x7FFF( %hd)\n", 0x7FFF); printf("16bit 0x8000(%hd)\n", 0x8000); printf("32bit 0x7FFFFFFF( %d)\n", 0x7FFFFFFF); printf("32bit 0x80000000(%d)\n", 0x80000000); printf("64bit 0x7FFFFFFFFFFFFFFF( %lld)\n", 0x7FFFFFFFFFFFFFFF); printf("64bit 0x8000000000000000(%lld)\n", 0x8000000000000000); }
[此贴子已经被作者于2016-4-21 08:11编辑过]
16位编译器 char 1个字节 char* 2个字节 short int 2个字节 int 2个字节 unsigned int 2个字节 float 4个字节 double 8个字节 long 4个字节 long long 8个字节 unsigned long 4个字节 32位编译器 char 1个字节 char* 4个字节 short int 2个字节 int 4个字节 unsigned int 4个字节 float 4个字节 double 8个字节 long 4个字节 long long 8个字节 unsigned long 4个字节 64位编译器 char 1个字节 char* 8个字节 short int 2个字节 int 4个字节 unsigned int 4个字节 float 4个字节 double 8个字节 long 8个字节 long long 8个字节 unsigned long 8个字节