-13

Please explain me the outcome of this code.

//code a when I run this code on my laptop, value of y is 4. And I think, logically value of y should be 5 because by doing x++ it should return 2 without incrementing as it is post increment and then when we add x which now contains an incremented value ie 3. So 2+3 is 5, according to me. But according to my turbo c++ 3.0 answer is 4.

#include <stdio.h>
void main() {
  int x = 2,y;
  **int y = x++ + x;**         // ans 4
  printf("y is :%d", y);
}

// code B When I run this code, the answer is 6. Here turbo c++ 3.0 in ++x is picking up an incremented value of x++, which is the opposite of above mention code. Logically here answer is correct but in the above code it's not.

#include <stdio.h>
void main() {
  int x = 2,y;
  **int y = x++ + ++x;**       //and 6
  printf("y is :%d", y);
}
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
  • Did you run it and find out? Did you run it i a debugger? What confuses you? – abligh Nov 02 '15 at 21:59
  • The whole point of the teacher assigning this exercise is for you to figure it out on your own, by studying the rules of operator order and precedence. Getting us to explain it to you defeats the purpose. – Barmar Nov 02 '15 at 22:06
  • Please see https://en.wikipedia.org/wiki/Comma_operator – OneCricketeer Nov 02 '15 at 22:07
  • voted to reopen: the question is closed with the reason "does not contain a MCVE" - but in fact it does contain a MCVE – M.M Nov 02 '15 at 22:27
  • @Barmar rules of sequence points (or sequencing in C11) are also required, which are usually explained poorly in school – M.M Nov 02 '15 at 22:29
  • 2
    @M.M: The close reason contains other criteia which are missing in the question. – too honest for this site Nov 02 '15 at 22:45
  • Homework/assignment trash. – Martin James Nov 02 '15 at 22:59
  • 1
    i have posted a new code with detailed explanations. Please tell me the difference between the two. Thank You everyone. I hope this code will help you guys to understand what i am actually asking for. – user3719983 Nov 03 '15 at 18:02

2 Answers2

0
  1. First program where the result is 4

    #include <stdio.h>
    
    void main()   {
     int x = 2,y;
     **int y = x++ + x;         // x=2, so y=2+2=4
     printf("y is :%d", y); // after post increment x=3
    }
    

Since a variable only gets it post incremented value only after the execution of the statement is completed so y=2+2

  1. Second program where the result is 6.

    Associativity of ++ operator is "right to left"

    void main()   {
       int x = 2,y;
       **int y = x++ + ++x;   //Since associativity is left to right so y=3+3=6
       printf("y is :%d", y);// after the post increment x=4
    }
    

Here the pre-increment is performed first since because of the associativity rule so y=3+3

ruchita
  • 163
  • 2
  • 11
0

Firstly, assignment operator that is = works from right to left, which means if you write x = 2 + 4 + 1; your compiler starts reading it from rightmost digit that is 1 then it add 4 to it and so on and then it assigns that value to x.

So, in your case statement y = x++ + x; compiler starts seeing it from right that is it first sees x i.e. 2 and then sees x++ i.e. also 2 as it is post increment operator finally it adds them and assigns y as 2 + 2 that is 4.

In the second case, that is y = x++ + ++x;, compiler first looks at ++x and as it is pre increment operator it increases x with one, i.e. x is now 3. After that x++ is seen and as stated above because it is post operator it would be treated as x in this operation and that value is 3 (remember we incremented x by one earlier) and hence, compiler assigns 3 + 3 i.e. 6 to y.

Tony Babarino
  • 3,355
  • 4
  • 32
  • 44
Yash Jain
  • 1
  • 1