且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 "int x = 5;printf("%d %d %d", x==5, x=10, x==5);";在 C 中打印“0 10 0"?

更新时间:2022-11-21 12:51:56

这确实是未定义的行为.函数的参数以未指定的顺序计算,因此执行任何依赖于该顺序的操作都会成为 UB.

This is indeed undefined behavior. Arguments to functions are evaluated in an unspecified order, so doing anything that relies on that order becomes UB.

看起来你的编译器是从右到左的(至少在这种情况下).这是一个合理的方法.但既然是 UB,不要指望它总是这样做.

It looks like your compiler goes right-to-left (at least in this instance). That's a reasonable way to do it. But since it's UB, don't count on it always doing that.