[问题] implicit conversion

楼主: ManOfSteel (Man Of Steel)   2022-05-01 05:24:09
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没人才惹。
作者: oToToT (屁孩)   2022-05-01 05:49:00
至少c是unsigned就会在比较时把a*b的结果转unsigned了吧
作者: karmel (吴承宇)   2022-05-01 05:54:00
同楼上 可以把a*b的结果print出来看看
作者: firejox (Tangent)   2022-05-01 08:59:00
阿不是补充说明都说完规则了,为什么会觉得是转成signedtype (′・ω・`)If the unsigned type has conversion rank greater than or equal to the rank of the signed type, then theoperand with the signed type is implicitly convertedto the unsigned type会转成 signed type 也就只有这段 If the signed type can represent all values of the unsigned type, then the operand with the unsigned type is implicitly converted to the signed type
作者: wulouise (在线上!=在电脑前)   2022-05-07 14:54:00
你开-Wall找warning看就知道了
作者: OnlyRD (里巷人)   2022-05-15 20:18:00
问题太低级,连回答都懒。

Links booklink

Contact Us: admin [ a t ] ucptt.com