Increment and Decrement operator in c??

Please help me guys for ??
i am always confuse while i am facing a problem related to pre and post - increment & decrements operators in c ??
i know basic rule of pre and post increment but after this i am again facing problem to solve it …
foe example –

1.>

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

2.>

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

3.>

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

according to me its output will be 6 7 8 but it becomes 8 8 8 why ??
4.>

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

according to me its output will be 5 6 7 but it becomes 7 6 5 why ??

Thank You guys.

The order of evaluation of pre and post increment is not defined for printf statements and the answer is compiler dependent. This is a question similar to this. You can check it out. Also check out other posts on Stackoverflow

1 Like

when we are working with printf function, if it is binary then evoluation happens from right to left…

  1. 8 7 6
  2. 7 6 5
  3. 8 7 6
  4. 7 6 5

In pre-increment and decrement operation, first all pre-increment is evaluated and then other operators are evaluated.(It has higher precednce)

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

All three pre-incements, are part of a single statement. So they are evaluated first before they are passed to printf as argument. After three increments, the value of ‘x’ changes to 6 then to 7 and then finally to 8. After that, when they are passed as arguments,the value of x is 8 and three copies of x are passed as arguments.

p.s. This works in most compilers where pre-increment operator has higher precedence. But still,it is compiler dependent.

your 3> code is giving 8 8 8 while in my pc it is giving 8 7 6 so it’s clear that it is compiler dependent but i can explain how my pc is working:-
printf is a function and each function have its own STACK to maintain the local variables.
x=5;
printf("%d %d %d",++x,++x,++x);
first time when ++x executed it change x=6 and store on the stack
second time when ++x executed it change x=7 and store on the stack
and as well 3rd time it change x as 8 and store on the stack
stack is [6 ,7 ,8,]
now semicolon will terminate the function and it will print by POP the variables as 8 7 6 .


YOUR LAST CODE ALSO JUSTIFYING MY FLOW OF CONTROL AND U’LL FIND HOW UR LAST CODE IS GIVING 7 6 5 INSTEAD OF 5 6 7

I think it’s not because of stack. once checkout stdarg.h

if the function contains variable number of arguments the arguments are processes in right to left fashion

if u are using a gcc compiler then output explaination is like this.Firstly increment the overall value and place the obtained value at expression which is not post incr/decr and for the rest calculate from right to left.As in your case3. ++a,++a,++a incremented value is 8 so place at everywhere.
case 2.a++,a++,a++ overall incrremented value is 8 but there is no any pre and actual expression so operation is from right to left and u get 7,6,5.
case 1.a++,++a,a here overall incremented value is 7 so put 7 at ++a and a.And start calculating from right to left,so output will be 6 7 7.

if u are using a gcc compiler then output explaination is like this.Firstly increment the overall value and place the obtained value at expression which is not post incr/decr and for the rest calculate from right to left.As in your case3. ++a,++a,++a incremented value is 8 so place at everywhere.
case 2.a++,a++,a++ overall incrremented value is 8 but there is no any pre and actual expression so operation is from right to left and u get 7,6,5.
case 1.a++,++a,a here overall incremented value is 7 so put 7 at ++a and a.And start calculating from right to left,so output will be 6 7 7.

pre increment and post increment work same as long as you dont make changes to the variable. Otherwise, it goes like this - pre increment first goes to the memory increments the value and uses that in your expression. Where as post increment will first get evaluated and then go to memory and increment it.
So when you just print ++x, you always get incremented. but if you use it in an expression, like ++x +y you need to apply the above.

//