m0_68683916 2022-03-26 01:48 采纳率: 62.5%
浏览 38
已结题

多个自增自减问题的和

img

img


为什么同样的代码运算结果又不一样
#include<stdio.h>
int main()
{
int i=5,j=5;
int p,q;
p=(i++)+(i++)+(i++);
q=(++j)+(++j)+(++j);
pritf("%d,%d",p,q);
return 0;
}
这个p,q到底怎么算,为什么我算出来的是18,21

img

为什么我算出来

  • 写回答

2条回答 默认 最新

  • _GX_ 2022-03-26 06:17
    关注

    不要写这种代码,你写的这种代码属于undefined behaviour。C/C++标准没有规定运算符操作数的计数顺序,因此不同编译器得到结果不一样。
    https://en.cppreference.com/w/cpp/language/eval_order

    Order of evaluation of any part of any expression, including order of evaluation of function arguments is unspecified (with some exceptions listed below). The compiler can evaluate operands and other subexpressions in any order, and may choose another order when the same expression is evaluated again.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月26日
  • 创建了问题 3月26日