当将 == op 与有符号 var 和无符号文字一起使用时,GCC 不会发出警告
为什么 GCC 只对下面代码中的情况 1 和 3 发出警告,而不对情况 2 发出警告?
我正在使用 -Wall 和 -g 标志进行编译。
int main() {
unsigned int ui = 4;
int si = 6;
if (si == ui ) { // Warning comparison b/w signed and unsigned
printf("xxxx");
}
if (si == 2U ) { // No Warning --- WHY ???
printf("xxxx");
}
if (si > 2U ) { // Warning comparison b/w signed and unsigned
printf("xxxx");
}
return 0;
}
Why does GCC warn only for situations 1 and 3 and not 2 in the code below ?
I'm compiling with -Wall and -g flags.
int main() {
unsigned int ui = 4;
int si = 6;
if (si == ui ) { // Warning comparison b/w signed and unsigned
printf("xxxx");
}
if (si == 2U ) { // No Warning --- WHY ???
printf("xxxx");
}
if (si > 2U ) { // Warning comparison b/w signed and unsigned
printf("xxxx");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html :
-W转换部分:
由于
2U
是字面量,因此 gcc 知道:si < 0
,则(无符号)si >= 2^31
,因此s1 != 2U
。(unsigned) si
与si
具有相同的值,因此(unsigned) si == 2U
当且仅如果si == 2
。总之,比较带符号的
si
与文字2U
与比较si
与2
相同,即将si
转换为unsigned
不会改变si == 2U
的结果。如果与 32 位无符号 int 中最大的 2^32-1 (4294967295U) 进行比较,它无法用
int
表示,那么si
可能等于它即使si
本身为负数,这也可能不是您想要的,因此会使用-Wextra
选项生成警告。http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html:
-Wconversion section:
Since
2U
is literal, gcc know that:si < 0
, then(unsigned) si >= 2^31
, therefores1 != 2U
.si > 0
, then(unsigned) si
has the same value assi
, therefore(unsigned) si == 2U
if and only ifsi == 2
.In conclusion, comparing the signed
si
with literal2U
is the same as comparingsi
with2
, i.e., the result ofsi == 2U
would not be changed by convertingsi
tounsigned
.If you compare with 2^32-1 (4294967295U), the largest in 32-bit unsigned int, which is not representable in
int
, thensi
could be equal to it even ifsi
itself is negative, this may not be what you wanted, so a warning is generated with-Wextra
option.可能是因为在类型的有符号版本和无符号版本重叠的范围内与常量进行相等比较时没有歧义。
如果我将其更改为
如果(si==2147483648U){
printf(“xxxx”);
}
我收到警告
(实际上,在收到您报告的警告之前我必须添加 -Wextra)
Possibly because there's no ambiguity in an equality comparison with constant in the range where signed and unsigned versions of the type overlap.
If I change it to
if (si == 2147483648U ) {
printf("xxxx");
}
I get a warning
(Actually, I had to add -Wextra before I got the warnings you reported)
克里斯感谢您的回答。我认为这导致了原因。我最初的想法是 U 后缀会导致该文字提升为无符号类型,但我认为只有当数字大于 INT_MAX_32 即 > 时,它才会提升为无符号类型。 2147483647。
Chris thanks for your answer. I think it leads to the cause. My original thought was that the U suffix would cause this literal to be promoted to an unsigned type however I think that it is only promoted to an unsigned type when the number is greater than INT_MAX_32 that is > 2147483647.