#include<stdio.h>
#include<stdlib.h>
int main() {
int a=3;
//case 1
printf("%d %d %d\n",(--a),(a--),(--a));//output is 0 2 0
printf("%d\n",a);//here the final value of 'a'
//end of case 1
a=3;
//case 2
printf("%d\n",(++a)+(a--)-(--a));//output is 5 value of a is 2
printf("%d\n",a);//here the final value of 'a'
//end of case 2
a=3;
//case 3
printf("%d\n",(++a)*(a--)*(--a));//output is 48 value of a is 2
printf("%d\n",a);//here the final value of 'a'
//end of case 3
//case 4
int i=3,j;
i= ++i * i++ * ++i;
printf("%d\n",i);//output is 81
i=3;
j= ++i * i++ * ++i;
printf("%d\n",j);//output is 80
//end of case 4
return 0;
}
I am clear with how these outputs are coming as I spent nearly 3 hours in it gazing at it , but all I want to know in detail is that why it is being taken or evaluated in this manner.
This was tricky one to judge printf evaluates from right to left so first --a pushes 2 , next a-- pushes 2 again as it is post then a becomes 1 , then --a makes first a 0 and then pushes that to and all i guessed that the output would be 0 2 2 but to my surprise it was 0 2 0 then i get it that the final value of a=0 is given to all the places where i used pre increment and decrement . Why this happens ?
This i take it as normal evaluation and then printing a is made 4 then in a-- again a is taken as 4 and again in --a a is taken as 3 so 4+4-3=5 done then final value of a happens to be one -1 from the post decrement that was done in the middle so a becomes 2 again Why this is happening ?
This execution is same as above and takes 4 * 4 * 3 = 48 and final a value is 2.
This is not tricky but i want the answer to it in detail what i figured is that first i becomes 4 in ++i and at i++ i becomes 4 and then at ++i i becomes 5 and so then 4*4*5 = 80 in this case i store it in i itself and then so i becomes 80 and then one +1 for the post increment. So i can one clear decision when i store 80 in another variable j and then saw it was 80 and the final i value was 6.
So logically I judged from just printing and viewing things in all these cases and are yet more which are completely based in this to come i can go on posting SO WHAT I WANT IS THAT AT ASSEMBLY LEVEL HOW IT IS HAPPENING AND WHY like IT TAKES POST INCREMENT AS A SEPARATE STATE OR SOMETHING how can different programs like this can be judged generically for each i cannot keep printing and finding the pattern can somebody help me in detail here. And the worst part was that i just executed the code in ideone and gives me completely different output you can even test at ideone if you are interested so why these happens please someone tell me i am breaking my head at this thing for 3 hours help me professionals.And i used gcc compiler.