C++ gcc:警告:大整数隐式截断为无符号类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2151305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gcc: warning: large integer implicitly truncated to unsigned type
提问by Abhijeet Rastogi
#include<stdio.h>
int main()
{
unsigned char c;
c = 300;
printf("%d",c);
return 0;
}
Is the output in any way predictable or its undefined??
输出是否以任何方式可预测或未定义?
回答by AraK
Sorry for the first answer, here is an explanation from the C++ standards :)
对不起,第一个答案,这里是 C++ 标准的解释:)
Is the output in any way predictable or its undefined??
输出是否以任何方式可预测或未定义?
It is predictable. There are two points to look after in this code:
First, the assignment of value that the type unsigned char
can't hold:
这是可以预见的。这段代码有两点需要注意:一是类型unsigned char
不能容纳的值的赋值:
unsigned char c;
c = 300;
3.9.1 Fundamental types (Page 54)
Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.41)
...
41) This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.
3.9.1 基本类型(第 54 页)
声明为无符号的无符号整数应遵守算术模 2n 的法则,其中 n 是该特定整数大小的值表示中的位数 。41)
...
41) 这意味着无符号算术不会因为结果溢出不能由结果无符号整数类型表示的值以比结果无符号整数类型可以表示的最大值大 1 的数为模减少。
Basically:
基本上:
c = 300 % (std::numeric_limits<unsigned char>::max() + 1);
Second, passing %d
in the format string of printf
to print unsigned char
variable.
This one ysthgot it right ;) There is no undefined behavior, because a promotional conversion from unsigned char to int happens in the case of variadic arguments
!
其次,传入to print变量%d
的格式字符串。
这个ysth做对了 ;) 没有未定义的行为,因为从 unsigned char 到 int 的促销转换发生在!printf
unsigned char
variadic arguments
Note: that the second part of the answer is a rephrasingof what have been said in the comments of this answerbut it is not my answer originally.
注意:答案的第二部分是对本答案评论中所说内容的重新表述,但这不是我最初的答案。
回答by UncleBens
The result of the assignment should be predictable:
分配的结果应该是可预测的:
3.9.1
3.9.1
4 Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2nwhere n is the number of bits in the value representation of that particular size of integer.17)
17) This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.
4 声明为无符号的无符号整数应遵守算术模 2 n的法则,其中 n 是该特定整数大小的值表示中的位数。17)
17) 这意味着无符号算术不会溢出,因为无法由结果无符号整数类型表示的结果以比结果无符号整数类型可以表示的最大值大一的数为模减少。
In addition, sizeof(char) is defined as 1, and sizeof(unsigned char) = sizeof(char), so you should see the same result regardless of implementation (assuming you don't have bytes with funny sizes other than 8).
此外,sizeof(char) 被定义为 1,并且 sizeof(unsigned char) = sizeof(char),因此无论实现如何,您都应该看到相同的结果(假设您没有 8 以外的有趣大小的字节)。
However, the warning is telling you that the result is probably not what you intended (for example, perhaps you overestimated the size of the unsigned type?). If that's what you intended, why not write 300 % (1 << CHAR_BIT)
(assuming that 300 is somehow significant to you)?
但是,警告告诉您结果可能不是您想要的(例如,您可能高估了无符号类型的大小?)。如果这就是您的意图,为什么不写300 % (1 << CHAR_BIT)
(假设 300 对您来说很重要)?