• closed by
8,045 views
3 3 votes
closed with the note: Undefined behaviour. Writing undefined code is against C language and such code won't come in standard exams like GATE
#include<stdio.h>
int main()
{
    int i = 1;
    printf("%d %d %d\n", i++, i++, i);
    return 0;
}
 on running i m getting answer : 2 1 3 why answer cant be 1 2 3

1 Answer

0 0 votes

  printf("%d %d %d\n", i++, i++, i);

In the above statement, the arguments i++ & i++ are the increment operators and the argument i is simply printing the value of i

For the increment/decrement operator the associativity is from right to left.

So, the second i++ will be evaluated first & will print 1 & increment i to 2

The first i++ will be evaluated next & will print 2 & increment i to 3

Lastly, i will be printed i.e. 3

So, answer is 2 1 3

Position:
Show:

No related questions found