printf() print arguments in reversed order
lhmouse
lh_mouse@126.com
Thu Dec 8 11:46:00 GMT 2016
> On 8 December 2016 at 07:13, lhmouse wrote:
>> It is undefined behavior.
>>
>> The evaluation of three parameters in question is (at the moment) unsequenced.
>> Since each of them contains a function call, the three function calls are also
>> unsequenced. The modify-and-read operations inside the function calls are
>> unsequenced relative to each other. Hence the UB.
>
> No, that's incorrect. The functions don't interleave. It's unspecified
> which order they execute in, but there is no undefined behaviour.
I did ask a similar question on ##c++ on freenode and it seemed that people couldn't
agree with each other. There were even opinions suggesting 'unspecified' be replaced
with 'undefined' because it could be hard to distinguish it from the other.
Personally I think his code is UB because `printf` is a library function call hence
is considered a 'side effect'. In spite that function calls don't interleave, changes of
order of evaluation could produce different side effects and the result is undefined.
------------------
Best regards,
lh_mouse
2016-12-08
More information about the Gcc-help
mailing list