这两个问题都涉及关于C语言sequence point(顺序点)。可以看看这个帖子http://www.chinaunix.net/old_jh/23/310576.html了解。
详细的定义可以在C语言标准的6.5章Expressions找到。
简单的说,像(a++)+(a++)+(a++)+(a++)这样的表达式对于C语言来说是未定义的,也就是一个不合乎标准的表达式。因为在多个a++表达式之间没有序列点来保证side effect的顺序。
如果你用gcc编译器对类似的表达式进行编译,就能够看到有警告关于sequence point.
[wangw33@localhost tmp]$ cat main.c
int main()
{
int a = 0;
int x = (a++) + (a++);
return x;
}
[wangw33@localhost tmp]$ gcc -Wall -o main main.c
main.c: In function 'main':
main.c:4:23: warning: operation on 'a' may be undefined [-Wsequence-point] <---警告
int x = (a++) + (a++);
C语言
EXAMPLE In the function call
(*pf[f1()]) (f2(), f3() + f4())
the functions f1, f2, f3, and f4 may be called in any order. All side effects have to be completed before
the function pointed to by pf[f1()] is called.
可以说对于函数参数的计算是没有固定顺序的,编译器完全可以按从右到左的顺序去计算参数的值,这样就会产生你看到的现象。