Result of expression!!!!!

This is a discussion on Result of expression!!!!! within the C Programming forums, part of the General Programming Boards category; Hello... Please analyze the following expression.. Suppose, a = 5; a = (++a) + (a++); Final value of a is ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    Result of expression!!!!!

    Hello...

    Please analyze the following expression..

    Suppose, a = 5;

    a = (++a) + (a++);

    Final value of a is 13 ...

    but it should be 11 na...

    Somebody please explain how is it 13....??

    Regards,
    Rohit M S

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by rohitms View Post
    Hello...

    Please analyze the following expression..

    Suppose, a = 5;

    a = (++a) + (a++);

    Final value of a is 13 ...

    but it should be 11 na...

    Somebody please explain how is it 13....??

    Regards,
    Rohit M S
    It's 13 with your compiler on your operating system with a specific compiler version and build flags. Other compilers/versions/systems/flags could give other values. Anything can be considered valid, and it's well within any compiler's right to toast your hard drive for using undefined behavior.

    It's probably dangerous for me to explain why you got the result you did, but I can't help myself...

    I'm guessing that your compiler is generating machine code that takes a+1 (6), adds 5 to it (11), assigns that value back to a, and then increments a twice, much like the following well-defined code:
    Code:
    a = (a+1);
    a = a + a;
    ++a;
    a++;
    But remember this is only one possible interpretation of the statement (because any result is valid).

    On the other hand, if you substituted other variables for a, like this:
    Code:
    a = (++b) + (c++);
    you might get machine code which looks something like this:
    Code:
    a = (b+1);
    a = a + c;
    ++b;
    c++;
    (I know, that's C, not machine code, but imagine that each statement gets compiled to a single instruction or two).

    Since each variable (a, b, and c) is being modified only once between sequence points, these statements are valid and well-defined and give the expected results.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, rohitms!

    I added it up in my head and got 13, FWIW.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Just so you don't think that 11 or 13 is "the" correct result, here are other valid interpretations of the expression.

    Here's one interpretation using temporary variables at the machine code level (perhaps they're CPU registers):
    Code:
    int x = a + 1;
    int y = a;
    a = x + y;
    a = x;
    a = y + 1;
    Result: 6

    How about using only one temporary variable (or register) for the post-increment:
    Code:
    a = a + 1; // ++a
    int x = a; // result of a++
    a = a + x;
    a = x + 1; // a++
    Result: 7

    If the post-increment is calculated before the pre-increment the result changes again:
    Code:
    int x = a; // result of a++
    a = a + 1; // ++a
    a = a + x;
    a = x + 1; // a++
    Result: 6

    What if we do this instead?
    Code:
    int x = a; // result of a++
    a = a + 1; // ++a
    a = a + x;
    a = a + 1; // a++
    Result: 12

    Or another legal (but unlikely) interpretation:
    Code:
    a = 400;
    Result: 400

    As anduril462 and I pointed out already, the result is undefined, so any result that the compiler produces for your original statement is valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I test for a result, but any result over 1 fails
    By jeremy duncan in forum C++ Programming
    Replies: 10
    Last Post: 05-23-2012, 02:23 PM
  2. initializer expression list treated as compound expression
    By karthikeyanvisu in forum C Programming
    Replies: 7
    Last Post: 02-26-2011, 05:19 PM
  3. Replies: 2
    Last Post: 11-25-2009, 07:38 AM
  4. Help with result only...
    By Ervilha in forum C Programming
    Replies: 5
    Last Post: 08-19-2008, 12:29 PM
  5. What would be the result...
    By sreeramu in forum C Programming
    Replies: 8
    Last Post: 03-25-2008, 05:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21