我正在測驗一些 C 代碼并遇到了一個錯誤,讓我陷入了回圈。我最終創建了以下代碼作為測驗,結果讓我感到驚訝。
似乎 ~ 運算子以某種方式改變了值的解釋(即它的型別),因為除非型別轉換測驗失敗。任何有關正在發生的事情的見解都值得贊賞。
代碼:
int main()
{
unsigned short one=1, two=1, three=1;
one=~one;
printf("One: %hu Two: %hu\n",one,two);
printf("Test: %s\n",(one==~three)? "Pass": "Fail");
printf("Test: %s\n",(one==(unsigned short)~three)? "Pass": "Fail");
printf("Test: %s\n",(one==~(unsigned short)three)? "Pass": "Fail");
return 0;
}
輸出:
One: 65534 Two: 1
Test: Fail
Test: Pass
Test: Fail
uj5u.com熱心網友回復:
unsigned int one = 1;
~one; // This expression has type int. So it's likely to be 0xFFFFFFFE
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/427870.html
上一篇:如何獲取陣列指標的地址?
下一篇:Sizeof與C中的變數
