1

Following is the code that I run in ubuntu 13.10. Code:-

    #include<stdio.h>
    main()
    {
       int i=10,j=10;
       i=i++ + ++j;
       printf("i=%d j=%d\n",i,j);
       j=++i + j++;
       printf("i=%d j=%d\n",i,j);
    }

Output:-

    i=21 j=11 
    i=22 j=33

Logically,as per rules ans should be:-

    i=22 j=11
    i=23 j=35

And when I run this code in ubuntu 12.10 then i get correct ans i.e. above ans. Please explain what is happening??

Braiam
  • 67,791
  • 32
  • 179
  • 269
Muskaan
  • 13
  • 3
  • This would probably be better suited for Stack Overflow. That being said, appending ++ is called postfix increment and returns the original value of the variable, not including the increment. Prepending ++ is called prefix increment and will return the value of the variable after increment. Therefore, the first assignment becomes: i=10+11;. – Nathan Osman Mar 22 '14 at 05:24
  • @NathanOsman Ohh yes that i know but i want to know that why am i not getting the ans as per rules of pre-post increment?Please help me with it. – Muskaan Mar 22 '14 at 05:27
  • I just explained how each operator works. I tested your snippet in 14.04 and I get the expected outcome as I've described. – Nathan Osman Mar 22 '14 at 05:30
  • Ok so value of i is 21 due to i=10+11..rite??so value of i dont get incremented after evaluation of exprsession i=i++ + ++j;but as per rules value of i should get incremented by 1 due to i++ that means the final ans should be i=22 due to execution of i++ after expresssion(i=i++ + ++j) evaluation.So is this correct? – Muskaan Mar 22 '14 at 05:46
  • i tested it and the results are: i=22 j=11 and i=23 j=35.. i = (i = i + 1) + ++j; i = (i = 10 + 1) + (j = j + 1); i = 11 + 11; so i = 22 j = 11.. – rusty Mar 22 '14 at 06:23

1 Answers1

0

That's the typical result of undefined or implementation dependent behavior. When your expression is altering a variable on both sites of the equation (among many other situations), you are basically doing it wrong. Just because it "works" with one compiler release does not mean it will give the same answer in the next release. Read the language specification carefully to see what sort of things are undefined or implementation dependent, and avoid them. Neither answer is "correct", they are undefined. Different answers may even be triggered by different optimizations of the same compiler.


In practice, you should use your compiler warning flags and a static analysis tool like valgrind or Purify to flag these sorts of things. And until you understand the meaning of terms like "Lvalue" and "Rvalue", just using an operator precedence table is not really sufficient to understand what's going on with some of the pre/post fix incre/decre operators.

ubfan1
  • 17,838
  • Ok sure.Thanks.But which ans is correct?The ans that i get while i compile the code or the ans which i get by understanding the rules of Increment Decrement Operator?please explain me... – Muskaan Mar 22 '14 at 05:30
  • I don't think that's right! "... expression is altering a variable on both sides of the equation ..." - I don't think that should be a problem, the language even support operators like ++, += among others which use same variable on the two sides of the assignment operator, eg. i++; is i = i + 1; – rusty Mar 22 '14 at 10:31
  • ..and if that's about compiler, I tested with gcc and Trubo C, both resulted in i=22 j=11, i=23 j=35. Can you suggest any compiler that handles it differently? – rusty Mar 22 '14 at 10:41