Rookie worker
菜鳥工程師的學習心得筆記
2012年6月3日 星期日
C語言 - 簡單小考題(2) - C強制轉型
What's the output in this function?
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") : puts("<= 6");
}
The answer is "> 6"
Why?
原因是當表達式中存在有符號類型和無符號類型時所有的操作數都自動轉換為無符號類型(unsigned)。因此-20變成了一個非常大的正整數,所以該表達式計算出的結果大於6
Reference:
[1]
http://albert-oma.blogspot.tw/2012/05/c-0x10.html
`
沒有留言:
張貼留言
‹
›
首頁
查看網路版
沒有留言:
張貼留言