C中数字的默认数据类型是什么? [英] What is the default data type of number in C?
问题描述
在 C 中,
unsigned int size = 1024*1024*1024*2;
导致警告表达式中的整数溢出..."同时
which results a warning "integer overflow in expression..." While
unsigned int size = 2147483648;
结果没有警告?
第一个表达式的正确值是否默认为 int?在 C99 规范中哪里提到了?
Is the right value of the first expression is default as int? Where does it mention in C99 spec?
推荐答案
当使用没有任何后缀的十进制常量时,十进制常量的类型是第一个可以表示的,按顺序(当前的 C 标准,6.4.4常数 p5):
When using a decimal constant without any suffixes the type of the decimal constant is the first that can be represented, in order (the current C standard, 6.4.4 Constants p5):
- 内部
- 长整数
- long long int
第一个表达式的类型是 int
,因为每个值为 1024 和 2 的常量都可以表示为 int.这些常量的计算将在 int 类型中完成,结果将溢出.
The type of the first expression is int
, since every constant with the value 1024 and 2 can be represented as int. The computation of those constants will be done in type int, and the result will overflow.
假设 INT_MAX 等于 2147483647 且 LONG_MAX 大于 2147483647,则第二个表达式的类型为 long int
,因为该值不能表示为 int,但可以表示为 long int.如果 INT_MAX 等于 LONG_MAX 等于 2147483647,则类型为 long long int
.
Assuming INT_MAX equals 2147483647 and LONG_MAX is greater than 2147483647, the type of the second expression is long int
, since this value cannot be represented as int, but can be as long int. If INT_MAX equals LONG_MAX equals 2147483647, then the type is long long int
.
这篇关于C中数字的默认数据类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!