我編譯下面的C代碼:打印負INT只要產生錯誤的結果
#include <stdio.h>
int main()
{
int nEndIndex = -1;
printf("nEndIndex : %ld\n", nEndIndex);
return 0;
}
我用GCC 4.2.4編譯它,如下所示:
[[email protected] ~/junk]$ gcc -o test test.c
[[email protected] ~/junk]$ ./test
nEndIndex : 4294967295
[[email protected] ~/junk]$ gcc --version
gcc (GCC) 4.2.4
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
,因爲我想long
大於int
使用%ld
應該不成問題。因爲他們都是signed
爲什麼我得到這個輸出?
它爲我打印-1。 gcc版本4.6.3 – 2014-11-25 04:46:16
@RaghuSrikanthReddy:只有運氣。行爲是未定義的。 – 2014-11-25 04:51:04