Thread: Evaluation of arithmetic expression that involves increment operators

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Evaluation of arithmetic expression that involves increment operators

    Here is a simple code to evaulate an arithmetic expression :

    a=(++a)+(++a)+(a++)+(a++) where a is an integer . Taking an initial value of a=10,we have,

    a= (++10)+(++a)+(a++)+(a++)
    = 11+(++11)+(a++)+(a++)
    = 11+12 +(12++)+(a++)
    = 11+12+12+(13++)
    = 11+12+12+13
    a= 48

    A simple Code executed using Turbo C++3.0 on Windows OS gives an output of 50 for the same.

    CODE:
    Code:
    #include<stdio.h>
    void main()
    {
    int a=10;
    a=(++a)+(++a)+(a++)+(a++);
    
    printf("%d",a);
    
    }
    As already said when this code is executed the value of a is printed as 50.

    Thus i am most certain that i have gone wrong in evaluating the expression myself as i have got 48 as the answer. Can anybody identify this error and tell me how this expression gets evaulated?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    you shouldnt try to work out this problem because your code is just poor and you should never have anything at all resembling that line in a real program.

    why?

    for the very reason that you are confused about this problem. you should only do one thing at a time. it makes your code clear. eg
    Code:
    x=y++;
    that is terrible code. does it mean?
    Code:
    x=y;
    y++;
    or
    Code:
    y++;
    x=y;
    the answer? who cares. we could compile and run it and find out but that isnt the problem. if anyone should ever ask that question about your code, then the code is ambigious and should be fixed so that it is absolutely clear what the program isa meant to be doing.

    (btw, if you are dying to know, your arithmetic is wrong. go re read what ++a and a++ do. its a little different to what you thikn they do, which is clear after looking at your arithmetic)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Expressions are not necessarily evaluated left to right. So when you substitue the value of a in your evaulation, you and the implementation has a choice of when to do so. It might sub in 10 first for all the a's for example, or it might go in the order you chose, or something else.

    Additionally, a++ and ++a do three things -- they fetch a, they compute a+1, and they change the value of a. There is no guarantee that all three things happen one right after another; the compiler can mix in other operations between them. This is a problem one of the operations mixed in is assignment to a, because that assignment may simply be lost when the program assigned the result of a+1 (computed based on the original a) to a.

    C is made for optimizations like this. The problems here are resolved by stating that you cannot assign or change the value of a memory location more than once between two sequence points. Nor can you retrieve and assign to a memory location between two sequence points except when the retrieved value is used to compute the new value. Seqence points are either the end of an expression (usually marked by a ;, or one of the sequencing operators &&, ||, ,(comma), and ?:.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Quote Originally Posted by abc1000 View Post

    As already said when this code is executed the value of a is printed as 50.


    Thanks in advance.
    The ONLY answer, is, the result is undefined. Period. End of story.
    50 is not right. 48 is not right. 32.8765 is not right. It is undefined. Trying to figure out an actual number is a 100% waste of time.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You should never post/pre increment or decrement a variable within the same code statement. This will surely lead to undefined behavior.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Brain_Child View Post
    you shouldnt try to work out this problem because your code is just poor and you should never have anything at all resembling that line in a real program.

    why?

    for the very reason that you are confused about this problem. you should only do one thing at a time. it makes your code clear. eg
    Code:
    x=y++;
    that is terrible code. does it mean?
    Code:
    x=y;
    y++;
    or
    Code:
    y++;
    x=y;
    the answer? who cares. we could compile and run it and find out but that isnt the problem. if anyone should ever ask that question about your code, then the code is ambigious and should be fixed so that it is absolutely clear what the program isa meant to be doing.

    (btw, if you are dying to know, your arithmetic is wrong. go re read what ++a and a++ do. its a little different to what you thikn they do, which is clear after looking at your arithmetic)
    You're wrong. The OP code is wrong, but your example is not. The code:
    Code:
    x=y++;
    is perfectly fine and it means:
    Code:
    x = y;
    ++y;
    There's no uncertainty or implementation defined behaviour about it.

  7. #7
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by MisterIO View Post
    You're wrong. The OP code is wrong, but your example is not. The code:
    Code:
    x=y++;
    is perfectly fine and it means:
    Code:
    x = y;
    ++y;
    There's no uncertainty or implementation defined behaviour about it.
    This guy is right. What brain_child said is complete bull......... There would be no point in having the post/pre incremement operators if they couldn't be used that way. And there is a reason you have both post and pre increment operators.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  3. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM