作者ManOfSteel (Immemorial)
看板C_and_CPP
标题[问题] implicit conversion
时间Sun May 1 05:24:09 2022
Hi all
大家好,这里有个问题想请教
开发平台(Platform): (Ex: Win10, Linux, ...)
windows 10
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
Visual studio 2019,.c file
问题(Question):
unsigned int a=10;
int b=-2;
unsigned int c=1;
if(a*b>c)
{
printf("Hello\n");
}
else
{
prntf("No Hello\n");
}
预期的正确结果(Expected Output):
No Hello
错误结果(Wrong Output):
Hello
补充说明(Supplement):
ref:
https://en.cppreference.com/w/c/language/conversion
我的理解:
a和b的rank应该是相同的。
且根据integer promotion的规则,a*b应该是先各自promote成int再乘,怎麽会是promot
e成unsigned?
开个大决:感觉这里没人会阿XD,ptt没人才惹。
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 49.217.10.84 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1651353852.A.140.html
1F:→ oToToT: 至少c是unsigned就会在比较时把a*b的结果转unsigned了吧05/01 05:49
2F:→ karmel: 同楼上 可以把a*b的结果print出来看看05/01 05:54
3F:→ firejox: 阿不是补充说明都说完规则了,为什麽会觉得是转成signed05/01 08:59
4F:→ firejox: type (′・ω・`)05/01 08:59
5F:→ firejox: If the unsigned type has conversion rank greater tha05/01 09:01
6F:→ firejox: n or equal to the rank of the signed type, then the05/01 09:01
7F:→ firejox: operand with the signed type is implicitly converted05/01 09:01
8F:→ firejox: to the unsigned type05/01 09:01
喔喔,原来还有这一段,感谢。
9F:→ firejox: 会转成 signed type 也就只有这段 If the signed type c05/01 09:06
10F:→ firejox: an represent all values of the unsigned type, then t05/01 09:06
11F:→ firejox: he operand with the unsigned type is implicitly conv05/01 09:06
12F:→ firejox: erted to the signed type05/01 09:06
而且我想我也是误解了integer promotion的意思,不过整体而言还是谢谢大大帮忙。If
int can "represent" the "entire range of values of the original type "(or the
range of values of the original bit field), the value is converted to type int
.
※ 编辑: ManOfSteel (49.217.10.84 台湾), 05/01/2022 17:24:22
13F:推 wulouise: 你开-Wall找warning看就知道了 05/07 14:54
14F:推 OnlyRD: 问题太低级,连回答都懒。 05/15 20:18