Is the output of printf (“%d %d”, c++, c); also undefined?

后端 未结 6 482
予麋鹿
予麋鹿 2020-11-27 07:25

I recently came across a post What is the correct answer for cout << c++ << c;? and was wondering whether the output of

int c = 0;  
printf (         


        
6条回答
  •  爱一瞬间的悲伤
    2020-11-27 07:57

    I have studied in lectures that post-fix and prefix operators increment value only after getting a semicolon

    This is not how the standard describes it. A sequence point is a point in code in which side effects which may have occurred in previous parts of the code have been evaluated. The comma between arguments to a function is not a sequence point, so the behavior there is undefined.

    The evaluation order of function arguments is unspecified. There is no guarantee that the arguments to a function will be evaluated in the order (1, 2, N), so there is no guarantee that the increment will be evaluated before the second argument is passed.

    So according to me, the output 0 0 is correct !!!

    No, the behavior is undefined, so you cannot reasonably claim that the output will be 0 0.

提交回复
热议问题