C4n 2022-09-29 18:48 采纳率: 80%
浏览 33
已结题

关于c语言的printf所出现的问题

他们两怎么会输出不同捏,就很奇怪啊,怎么正文还得三十字啊啊。

img

img

  • 写回答

4条回答 默认 最新

  • _GX_ 2022-09-29 19:52
    关注

    你的代码是Undefined behavior
    因为C/C++语言标准里没有规定函数参数的计算顺序,编译器可以按任意顺序来计算函数参数值。
    因此你代码中a=3和a=10的计算顺序是不确定的,得到的结果也就不一样。

    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.
    (1) If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined.

    i = ++i + i++; // undefined behavior
    i = i++ + 1; // undefined behavior
    f(++i, ++i); // undefined behavior
    f(i = -1, i = -1); // undefined behavior
    

    (2) If a side effect on a scalar object is unsequenced relative to a value computation using the value of the same scalar object, the behavior is undefined.

    f(i, i++); // undefined behavior
    a[i] = i++; // undefined bevahior
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 10月7日
  • 已采纳回答 9月29日
  • 创建了问题 9月29日