【发布时间】:2011-05-23 00:20:38
【问题描述】:
[cprg]$ cat test.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int i=10;
printf("i=%d\ni++=%d\n++i=%d\n",i,i++,++i);
return 0;
}
[cprg]$ make
gcc -g -Wall -o test test.c
test.c: In function ‘main’:
test.c:7: warning: operation on ‘i’ may be undefined
test.c:7: warning: operation on ‘i’ may be undefined
[cprg]$ ./test
i=12
i++=11
++i=12
我不知道为什么会发生这种情况。请问任何人都可以 详细解释一下这里发生了什么?
【问题讨论】:
-
不是
printf导致了奇怪的行为。 -
你应该知道,编写测试程序(我希望这是一个测试,而不是在生产代码中使用)是好的,但在一个编译器上获得预期(或意外)的结果并不能保证其他编译器的行为方式相同。 secure.wikimedia.org/wikipedia/en/wiki/Sequence_point阅读Sequence points in C and C++下的第4项
标签: c