closed by
7,222 views
3 votes
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
closed by

1 Answer

0 votes
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

Related questions

1 votes
1 votes
1 answer
1
tusharb asked Mar 1, 2022
429 views
Suppose we have a 32-bit memory and we have to representunsigned int a = -5What will the memory representation look like?000000…….1011or 111…..1011
2 votes
2 votes
2 answers
4
srestha asked May 13, 2019
961 views
Can someone explain the output of this code? and what (char*) is doing actually?#include<stdio.h struct Ournode{ char x, y, z; }; int main() { struct Ournode p={'1', '0',...