为什么这个是19,自加自减不是从右往左结合吗

img

差点就觉得你这是对的了

img


为什么会是19.....18才是对的呀
++a+a++ ++a后 a=9 9+9=18,最后的++是不会计算进去的哦
++a是先自增 a++是后自增

只定义了一个变量a,++a+a++这个式子从左向右++a为9注意这时候a这个变量的值也变成了9故在+a就是加9之后再将结果++所以算式为9+9+1

我也不懂,所以我一般不这么写

我的编译器答案是18

你这段代码属于undefined behavior,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.

知道了运算规律和运算优先级就可以了,没有必要这么较真。谁编程会这样写呢?这样写出来的代码怎么交流呢?另外,不同的编译软件也会有不同的编译结果。