关于一些C的基本类型值的疑惑
用的centos系统# cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)
# uname -r
3.10.0-1062.12.1.el7.x86_64
# getconf LONG_BIT
64
C的代码
#include <stdio.h>
int main(void){
int a=2147483647;
printf("INTtakes %lubytes!\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 inttakes %lubytes!\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 inttakes %lubytes!\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 inttakes %lubytes!\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 longinttakes %lubytes!\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;
}
运行结果:
INTtakes 4bytes!
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 inttakes 8bytes!
Lont Integer max value is 2147483647 ,After adding 1 "c" value is -2147483648
Unsigned Long inttakes 8bytes!
Ungigned Long Integer max value is -5 ,After adding 1 "d" value is -4
Unsigned inttakes 4bytes!
Unsigned Integer max value is 2147483647 ,After adding 1 "e" value is -2147483648
Long longinttakes 8bytes!
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, 情况也不对呀,为什么? https://blog.csdn.net/frank0712105003/article/details/8044903 格式化操作符用错了。
本帖最后由 永恒的蓝色梦想 于 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 inttakes %lubytes!\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 inttakes 4bytes!
Unsigned Integer max value is 2147483647 ,After adding 1 "e" value is -2147483648 unsigned e = 2147483647;
printf("Unsigned inttakes %lubytes!\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 来输出。
你是在考我们吗?{:5_94:}
满意请选最佳哦~{:10_297:} 顺便问一下,你字符串中都没转义,怎么过的编译? Hello. 发表于 2020-7-10 21:16
https://blog.csdn.net/frank0712105003/article/details/8044903
我感觉你并没有看问题,而只是复制了个链接{:10_277:} 永恒的蓝色梦想 发表于 2020-7-10 22:14
顺便问一下,你字符串中都没转义,怎么过的编译?
你是说这段吗?代码cpoy过来变了,能编译过,肯定没问题的!
Python用管了,不知道C格式化的时候,分数据来{:10_263:}
int -> %d
long -> %ld
long long ->%lld
char -> %s
unsigned ->%u
五花八门的格式化输出~{:10_288:}
printf("After adding 1 "d" value is %d\n\n",d); 本帖最后由 永恒的蓝色梦想 于 2020-7-11 08:40 编辑
Stubborn 发表于 2020-7-11 02:12
你是说这段吗?代码cpoy过来变了,能编译过,肯定没问题的!
Python用管了,不知道C格式化的时候,分 ...
这代码不可能过编译{:10_277:}
你用 cout 就不会出这种问题了……{:10_277:}
纠正,应该是:char -> %c
char* -> %s 本帖最后由 Stubborn 于 2020-7-11 11:20 编辑
永恒的蓝色梦想 发表于 2020-7-11 07:15
这代码不可能过编译
你用 cout 就不会出这种问题了……
纠正,应该是:
\"a\" 这里斜巷被转义了,代码里面有的
# cat se3.c
#include <stdio.h>
int main(void){
int a=2147483647;
printf("INTtakes %lubytes!\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 inttakes %lubytes!\nLont Integer max value is %ld ,",sizeof(c), c);
c = c + 1;
printf("After adding 1 \"c\" value is %ld\n\n",c);
unsigned long d = 2147483646 * 2;
printf("Unsigned Long inttakes %lubytes!\nUngigned Long Integer max value is %u ,",sizeof(d), d);
d = d + 1;
printf("After adding 1 \"d\" value is %u\n\n",d);
unsigned e = 2147483647;
printf("Unsigned inttakes %lubytes!\nUnsigned Integer max value is %u ,",sizeof(e), e);
e = e +1;
printf("After adding 1 \"e\" value is %u\n\n",e);
long long f=5;
printf("Long longinttakes %lubytes!\nLong long Integer max value is %lld ,",sizeof(f), e);
f = f +1;
printf("After adding 1 \"e\" value is %lld\n\n",f);
return 0;
}
# gcc se3.c -o se && ./se
se3.c: In function ‘main’:
se3.c:23:34: warning: integer overflow in expression [-Woverflow]
unsigned long d = 2147483646 * 2;
^
INTtakes 4bytes!
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 inttakes 8bytes!
Lont Integer max value is 2147483647 ,After adding 1 "c" value is 2147483648
Unsigned Long inttakes 8bytes!
Ungigned Long Integer max value is 4294967292 ,After adding 1 "d" value is 4294967293
Unsigned inttakes 4bytes!
Unsigned Integer max value is 2147483647 ,After adding 1 "e" value is 2147483648
Long longinttakes 8bytes!
Long long Integer max value is 2147483648 ,After adding 1 "e" value is 6
# Stubborn 发表于 2020-7-11 11:19
\"a\" 这里斜巷被转义了,代码里面有的
巧了,你的代码里看不见{:10_277:}
\"\"\"\" 永恒的蓝色梦想 发表于 2020-7-11 16:12
巧了,你的代码里看不见
\"\"
复制过来被转义了。 {:10_262:} Stubborn 发表于 2020-7-11 18:09
复制过来被转义了。
论坛的 BUG{:10_277:}
你在线啊{:10_297:} Stubborn 发表于 2020-7-11 18:09
复制过来被转义了。
其实你用 C++,完全不会出这种问题{:10_277:}
只要你不特别注重效率{:10_277:}
页:
[1]