问题
I've been studying C for about a year now, and I came across this above when I was just playing around. I first thought maybe it's a case of assignment precedence (i.e. x=10 happens first), but then I tried
printf("%d %d %d", x==5, x=10, x<6);
and it outputs
0 10 1
. Could someone please explain to me why/what is going, as this seems extremely baffling to me and I'm starting to think it's undefined behavior perhaps?
回答1:
This is indeed undefined behavior. Arguments to functions are evaluated in an unspecified order, so doing anything that relies on that order becomes 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.
来源:https://stackoverflow.com/questions/56086241/why-does-int-x-5-printfd-d-d-x-5-x-10-x-5-in-c-print-0-10-0