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

后端 未结 6 492
予麋鹿
予麋鹿 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 08:05

    The behaviour will be definitely undefined due to the undefined evaluation order of parameters. You can prove this "undefined output" doing some random testing:

    printf("%d %d\n", c++, c);
    // result: 0 1
    printf("%d %d %d\n", c++, c, c++);
    // result: 1 2 0
    printf("%d %d %d %d\n", c++, c++, c++, c);
    // result: 2 1 0 3
    printf("%d %d %d %d\n", c++, c, c++, c);
    // result: 1 2 0 2
    printf("%d %d %d %d\n", c++, c, c, c);
    // result: 0 1 1 1
    

提交回复
热议问题