0
#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.

  1. 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 ?

  2. 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 ?

  3. This execution is same as above and takes 4 * 4 * 3 = 48 and final a value is 2.

  4. 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.

Eric
  • 95,302
  • 53
  • 242
  • 374
  • Fascinating question! – David Elliman Aug 09 '13 at 19:14
  • @Daniel Fischer please there got to be some assembly level programmers who should have an idea why this is happening right I just have spent some time in this , even i read the answers that was posted above to other questions i cannot find the solution to the problem.How will one generically say like this will be the ways things will work always with these please someone explain at basic level even answers to the questions were all only based on what we see and was not like this only happens (no correctness).Anyone ?? – Krishna sundar Aug 09 '13 at 19:29
  • @Krishnasundar Reading these slides [Deep C](http://www.slideshare.net/olvemaudal/deep-c) may be helpful but at the end of the day you may be able to determine why a specific compiler does it one way but it could easily change with the next version or it may not even be totally predictable within one version based on optimization level etc... – Shafik Yaghmour Aug 09 '13 at 19:31
  • The compiler is free to do whatever it wants when encountering such code. Different compilers do different things. If you want to know what a particular compiler does, look at its assembly output, or the compiler's source code. The language says it's undefined behaviour, there is no standard or portable answer. – Daniel Fischer Aug 09 '13 at 19:32
  • @DanielFischer lol ... my the problem is , these are the things in which people are tested in my college for the C aps . They are expecting one answer for a particular snippet and the answer that each one gives will be different and they expect one particular answer based on it people are graded , so only i got a bit involved in this.So next time for such questions i can clearly mention the output regarding a particular compiler i know and just say that it depends only on the kind of compiler one uses right! – Krishna sundar Aug 09 '13 at 19:43
  • 2
    My sincerest commiserations. Expecting a particular answer to a question "how will this undefined behaviour manifest?" is worse than terrible. The output can depend on not only the compiler and version, platform, also on the compiler options passed (technically it could be different even with the same version, on the same platform, with the same options). – Daniel Fischer Aug 09 '13 at 19:51

1 Answers1

4

This is all undefined behavior modifying a variable more than one in between sequence points in this in this case within the same expression is not allowed. So you can not rely on the output of this program at all.

From the c99 draft standard 6.5.2:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

it cites the following code examples as being undefined:

i = ++i + 1;
a[i++] = i; 
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • @Daniel Fischer please there got to be some assembly level programmers who should have an idea why this is happening right I just have spent some time in this , even i read the answers that was posted above to other questions i cannot find the solution to the problem.How will one generically say like this will be the ways things will work always with these please someone explain at basic level even answers to the questions were all only based on what we see and was not like this only happens (no correctness).Anyone ?? – Krishna sundar Aug 09 '13 at 19:28
  • 1
    @Krishnasundar “there got to be some assembly level programmers who should have an idea why this is happening” You do not seem to have any idea how a modern optimizing compiler works and how unpredictable it can be for undefined programs. No, no-one can predict what your undefined code does. All of GCC's and ideone's and clang's different answers are correct. – Pascal Cuoq Aug 09 '13 at 19:31