来源:www.cncfan.com | 2006-4-28 | (有4329人读过)
kingwei 2005.3.10
实验环境: Dev-C++ 4.9.6.0 (gcc/mingw32), 使用-Wall编译选项
#include <stdio.h>
int main() { int v_int; signed int v_signed_int; unsigned int v_unsigned_int; signed short int v_signed_short_int; unsigned short int v_unsigned_short_int; signed long int v_signed_long_int; unsigned long int v_unsigned_long_int; freopen("intuex.txt", "r", stdin); freopen("out.txt", "w", stdout); /* [-2^31, 2^31-1] ==> [-2147483648, 2147483647] */ scanf("%d", &v_int); printf("%d\n", v_int); /* [-2^31, 2^31-1] ==> [-2147483648, 2147483647] */ scanf("%d", &v_signed_int); printf("%d\n", v_signed_int); /* [0, 2^32-1] ==> [0, 4294967295] */ scanf("%u", &v_unsigned_int); printf("%u\n", v_unsigned_int); /* [-2^15, 2^15-1] ==> [-32768, 32767] */ scanf("%hd", &v_signed_short_int); printf("%hd\n", v_signed_short_int); /* [0, 2^32-1] ==> [0, 65535] */ scanf("%hu", &v_unsigned_short_int); printf("%hu\n", v_unsigned_short_int); /* [-2^31, 2^31-1] ==> [-2147483648, 2147483647] */ scanf("%ld", &v_signed_long_int); printf("%ld\n", v_signed_long_int); /* [0, 2^32-1] ==> [0, 4294967295] */ scanf("%lu", &v_unsigned_long_int); printf("%lu\n", v_unsigned_long_int);
return 0; }
测试样例及输出:
----- test case #1: 下界 -----
-2147483648 -2147483648 0 -32768 0 -2147483648 0
output:
-2147483648 -2147483648 0 -32768 0 -2147483648 0
----- test case #2: 上界 -----
2147483647 2147483647 4294967295 32767 65535 2147483647 4294967295
output:
2147483647 2147483647 4294967295 32767 65535 2147483647 4294967295
----- test case #3: 下溢 -----
-2147483649 -2147483649 -1 -32769 -1 -2147483649 -1
output:
2147483647 2147483647 4294967295 32767 65535 2147483647 4294967295
----- test case #4: 上溢 -----
2147483648 2147483648 4294967296 32768 65536 2147483648 4294967296
output:
-2147483648 -2147483648 0 -32768 0 -2147483648 0
可以看到,输入数据下溢,成为该类型所能表示范围的上界;输入数据上溢,成为该类型所能表示范围的下界.
|