警告:在隐式常量转换中溢出 [英] Warning : overflow in implicit constant conversion
问题描述
在下面的程序中,第5行给出了预期的溢出警告,但令人惊讶的是第4行在GCC中没有给出任何警告:http://www.ideone.com/U0BXn
In the following program, the line 5 does give overflow warning as expected, but surprisingly the line 4 doesn't give any warning in GCC: http://www.ideone.com/U0BXn
int main()
{
int i = 256;
char c1 = i; //line 4
char c2 = 256; //line 5
return 0;
}
我想这两行应该给予溢出警告。
导致我做这个实验的话题是: typedef类型检查?
The topic which led me to do this experiment is this: typedef type checking?
我说下面的我从我的回答中删除,因为当我运行它,它没有显示出我的预期):
There I said the following(which I deleted from my answer, because when I run it, it didn't show up as I had expected):
//However, you'll get warning for this case:
typedef int T1;
typedef char T2;
T1 x = 256;
T2 y = x; //possible overflow warning! (but it doesn't give warning :()
推荐答案
-Wall
不包含许多选项。 -Wconversion
是其中之一,警告您感兴趣的行为
-Wall
doesn't include many options. -Wconversion
is one of them and warns about the behavior you're interested in.
请参阅 http:// gcc。 gnu.org/onlinedocs/gcc/Warning-Options.html
这篇关于警告:在隐式常量转换中溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!