int i=3,p,q;
p=(++i)+(++i)+(++i);
q=(i++)+(i++)+(i++);
printf("%d %d",p,q);
这个p的输出结果为什么是16不是15啊
应该都是18吧,因为先算增量,算完之后i的值变成6,6+6+6=18
另外据我所知,不同编译器对这种增量的处理方式不一样,所以没有必要纠结
这是个错误的代码,不同服务器结果可能不一样,在符合规则的情况下,会有几种不同的结果
C/C++语言标准没有规定运算符的操作数的计算顺序,编译器可以按任意顺序来计算操作数的值,因此如果一个变量在同一个运算符的两个操作数分别被修改或一个修改一个读取,其结果在不同的编译器上可能不一样的。以后不要写这种代码,讨论也没有意义。
From https://en.cppreference.com/w/c/language/eval_order
Order of evaluation of the operands of any C operator, including the order of evaluation of function arguments in a function-call expression, and the order of evaluation of the subexpressions within any expression is unspecified (except where noted below). The compiler will evaluate them in any order, and may choose another order when the same expression is evaluated again.