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日

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题