电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> C++/VC>>Dev-C++下关于几种int类型格式符的实验:

Dev-C++下关于几种int类型格式符的实验

来源:www.cncfan.com | 2006-4-28 | (有4157人读过)

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

可以看到,输入数据下溢,成为该类型所能表示范围的上界;输入数据上溢,成为该类型所能表示范围的下界.

C++/VC热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号