[問題] 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