|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
用的centos系统
- [root@izwz92o83jsu6x277ey7lzz SE2]# cat /etc/centos-release
- CentOS Linux release 7.7.1908 (Core)
- [root@izwz92o83jsu6x277ey7lzz SE2]# uname -r
- 3.10.0-1062.12.1.el7.x86_64
- [root@izwz92o83jsu6x277ey7lzz SE2]# getconf LONG_BIT
- 64
复制代码
C的代码
- #include <stdio.h>
- int main(void){
- int a=2147483647;
- printf("INT takes %lu bytes!\nInteger max value is %d ,",sizeof(a), a);
- a = a + 1;
- printf("After adding 1 "a" value is %d\n\n",a);
-
- short b=32767;
- printf("Short takes %lu bytes!\n Short max value is %d,", sizeof(b), b);
- b = b + 1;
- printf("After adding 1 "b" value is %d\n\n", b);
-
- long c = 2147483647;
- printf("Long int takes %lu bytes!\nLont Integer max value is %d ,",sizeof(c), c);
- c = c + 1;
- printf("After adding 1 "c" value is %d\n\n",c);
- unsigned long d = 2147483646 * 2 - 1;
- printf("Unsigned Long int takes %lu bytes!\nUngigned Long Integer max value is %d ,",sizeof(d), d);
- d = d + 1;
- printf("After adding 1 "d" value is %d\n\n",d);
- unsigned e = 2147483647;
- printf("Unsigned int takes %lu bytes!\nUnsigned Integer max value is %d ,",sizeof(e), e);
- e = e +1;
- printf("After adding 1 "e" value is %d\n\n",e);
-
- long long f=5;
- printf("Long long int takes %lu bytes!\nLong long Integer max value is %d ,",sizeof(f), e);
- f = f +1;
- printf("After adding 1 "e" value is %d\n\n",f);
- return 0;
- }
复制代码
运行结果:
- INT takes 4 bytes!
- Integer max value is 2147483647 ,After adding 1 "a" value is -2147483648
- Short takes 2 bytes!
- Short max value is 32767,After adding 1 "b" value is -32768
- Long int takes 8 bytes!
- Lont Integer max value is 2147483647 ,After adding 1 "c" value is -2147483648
- Unsigned Long int takes 8 bytes!
- Ungigned Long Integer max value is -5 ,After adding 1 "d" value is -4
- Unsigned int takes 4 bytes!
- Unsigned Integer max value is 2147483647 ,After adding 1 "e" value is -2147483648
- Long long int takes 8 bytes!
- Long long Integer max value is -2147483648 ,After adding 1 "e" value is 6
复制代码
问题1: long不是 4 byte 吗?为什么打印是8 byte,是什么原因导致的。
问题2: 8 byte的最大最小值为什么也是-2147483648 ~ 2147483647, 和 4 byte的int一模一样。
问题3 : unsigned 的最小值不应该是0吗?最大值应该是 2^32, 情况也不对呀,为什么?
本帖最后由 永恒的蓝色梦想 于 2020-7-10 22:12 编辑
问题1: long不是 4 byte 吗?为什么打印是8 byte,是什么原因导致的。 有些机器 long 就是 64bit 。建议不要使用 long,用 int 和 long long 代替。
问题2: 8 byte的最大最小值为什么也是-2147483648 ~ 2147483647, 和 4 byte的int一模一样。 Lont Integer max value is 2147483647 ,After adding 1 "c" value is -2147483648 - long c = 2147483647;
- printf("Long int takes %lu bytes!\nLont Integer max value is %d ,",sizeof(c), c);
- c = c + 1;
- printf("After adding 1 "c" value is %d\n\n",c);
复制代码8bit 整形用 %d 格式化发生截断,前 4bit 数据丢失,所以出现这样的结果。
问题3 : unsigned 的最小值不应该是0吗?最大值应该是 2^32, 情况也不对呀,为什么? Unsigned int takes 4 bytes!
Unsigned Integer max value is 2147483647 ,After adding 1 "e" value is -2147483648 - unsigned e = 2147483647;
- printf("Unsigned int takes %lu bytes!\nUnsigned Integer max value is %d ,",sizeof(e), e);
- e = e +1;
- printf("After adding 1 "e" value is %d\n\n",e);
复制代码int 型的 -2147483648 和 unsigned 型的 2147483648 在内存中表示是完全一样的。在这里,你使用 %d 格式化,所以是按 int 来输出。
你是在考我们吗?
满意请选最佳哦~
|
|