[問題] implicit conversion

看板C_and_CPP (C/C++)作者 (Immemorial)時間2年前 (2022/05/01 05:24), 2年前編輯推噓0(0012)
留言12則, 3人參與, 2年前最新討論串1/1
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://www.ptt.cc/bbs/C_and_CPP/M.1651353852.A.140.html

05/01 05:49, 2年前 , 1F
至少c是unsigned就會在比較時把a*b的結果轉unsigned了吧
05/01 05:49, 1F

05/01 05:54, 2年前 , 2F
同樓上 可以把a*b的結果print出來看看
05/01 05:54, 2F

05/01 08:59, 2年前 , 3F
阿不是補充說明都說完規則了,為什麼會覺得是轉成signed
05/01 08:59, 3F

05/01 08:59, 2年前 , 4F
type (′・ω・`)
05/01 08:59, 4F

05/01 09:01, 2年前 , 5F
If the unsigned type has conversion rank greater tha
05/01 09:01, 5F

05/01 09:01, 2年前 , 6F
n or equal to the rank of the signed type, then the
05/01 09:01, 6F

05/01 09:01, 2年前 , 7F
operand with the signed type is implicitly converted
05/01 09:01, 7F

05/01 09:01, 2年前 , 8F
to the unsigned type
05/01 09:01, 8F
喔喔,原來還有這一段,感謝。

05/01 09:06, 2年前 , 9F
會轉成 signed type 也就只有這段 If the signed type c
05/01 09:06, 9F

05/01 09:06, 2年前 , 10F
an represent all values of the unsigned type, then t
05/01 09:06, 10F

05/01 09:06, 2年前 , 11F
he operand with the unsigned type is implicitly conv
05/01 09:06, 11F

05/01 09:06, 2年前 , 12F
erted to the signed type
05/01 09:06, 12F
而且我想我也是誤解了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
文章代碼(AID): #1YRQZy50 (C_and_CPP)
文章代碼(AID): #1YRQZy50 (C_and_CPP)