i++ and ++i confusion

i have a code like this

#include<stdio.h>
void main()
{
    int i=5;
    printf("%d %d %d ",i++,i++,i++);
}

output is 7 6 5 , i understand that but when i tried this code

#include<stdio.h>
void main()
{
    int i=5;
    printf("%d %d %d ",++i,++i,++i);
}

according to me output should be 8 7 6 but
output is 8 8 8 , i am confused now.

2 Likes

This a usually differs with the compiler with which we are using.
Moreover in gcc compiler ++i is given more preference.Thereby first the values are incremented by 1 from the right to the left.That is the first tym it becomes 6,then 7,then finally 8. Then it is printed in the order from left to right.
I repeat this ‘MAY’ not be the case with some other compiler.Hope you got it.

2 Likes

The output from multiple use of same kind of operators on the same operand during parameter passing can never be predicted and is said to be compiler specific!

1 Like

i++ operator returns a copy of the earlier value whereas ++i returns a reference to the number itself after increment.

Here by the time, ++i is output to the terminal, it’s value has been changed by two computations. So you are getting 8,8,8. If you can somehow flush the result to output after one operation, you will get your desired result.

2 Likes

Ha! I have an explanation (for the gcc compiler). But it seems to fit!!

Note: I am copying my own answer from a fb group. So, I am using the following code snippet as reference:

int i=10;
printf("%d,%d,%d,%d",i++,++i,i++,++i);

Pre-increment operators work like, they first update, and then, we get to use the value. Post-increment does it like, we get to use the value first and then update.

Again, on a stack-based function evaluation model, the operands get ‘evaluated’ right-to-left. So, the first expression that we get is ++i. That increments the value of i to 11. But since, this is a pre-increment, we won’t use the value, until the whole bunch of things get evaluated. So, we need to put in the value of i as the 4th argument, which will happen at the very end.

Then, we have an i++, so, we use the value we have in hand, which is 11. Hence the value 11 as the third one. We, of course do update i to 12.

Then, we have a ++i again, so we just increment the value of i to 13.

Then, we have an i++, and we use the value 13, to get the first number as 13. The remaining update takes i to 14.

Now, since everything that can be done has been done, we need to plug in the values of i wherever they were required (due to the pre-increment). So, we have two 14s, at the second and fourth place.

That gives us the answer 13, 14, 11, 14.

In fact, the same argument holds up for these following as well.

int i = 10;
printf("%d, %d, %d, %d\n", i++, ++i, ++i, i++);
printf("%d, %d, %d, %d\n", --i, i--, i--, --i);

will give you

13, 14, 14, 10
10, 12, 13, 10

I hope, this clears your doubts.

But, this explanation only fits for the gcc compiler. For other compilers. the answer might vary and will need another explanation.

So, the expected answer for such problems will vary from compiler to compiler. But, the above one seems to fit for gcc compiler.

Now, you can easily see, why

int i=5;
printf("%d %d %d ",++i,++i,++i);

gives the answer as 8 8 8.

7 Likes

in pre increment operation (a++) solve left to right and assign value also left to right
in above code i=5 and i++,i++,i++ that mean solve left to right nd assign value than output -6,7,8

but in post increment first solve left to right but not assign value in post incremnent value assign rignt to left
in above code ++i,++i,++i
i=5 first i increment 3 times that meani=8 after that assign all 3 i =8 so \output=8,8,8

pre increment first change then use it whereas post increment first use then change it…

therefore if i=5 ++i,++i,++i ans will be 8,8,8

what about
i=5
int y=i-- + i-- + i–
printf("%d",y);

What will be output of the following “c” code?

#include
void main ( )
{
int i=5;
printf("%d %d %d %d %d", i++, i–,++i, --i, i);
}

please expain as above…@ [tijoforyou]

In this case too there will be stack evaluation of operators from right to left. For post increment/decrement operators, the value will increase or decrease differently than pre-increment/decrement operators.
Right->left:
i=5;
–i=4;
++i=5;
Both pre increment operators will print 5 as ++i is evaluated later and printed first(value used later).
i–=5, first value used then decremented.(Now i=4)
i++=4 same as i–.

Therefore, the output should be 4,5,5,5,5. But again, this is compiler specific, so no promises!
Hope this helps.

LOL…
This is famous question. Read about undefined behaviour and sequence points. Basically it is voilating one of the rules of sequence points listed on wikipedia.

1 Like