我编译下面的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