Thread: preincrement

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    13

    preincrement

    Hello

    Code:
    #include <iostream.h>
    int main()
    {
    	int c=3;
    	cout << (((++c)) + ((++c))) << endl;
    	return 0;
    }
    The output is 10. Why? In Java/PHP the result is 9. For me it`s 9 too. Can i find the reason in the priority rules?

    Thanks
    sphreak

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    The ++ is processed before the + operator, and since there are two, this is the order of ops:

    ++c
    ++c
    c+c

    making:

    ++c---c is now 4
    ++c---c is now 5
    c+c---result 10

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The expression is ambiguous.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    I just did a search, msdn's got the order of ops documented:

    http://msdn.microsoft.com/library/de...evaluation.asp

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Bjarne Stroustrup says that for the post-increment case this expression is undefined:
    http://www.research.att.com/~bs/bs_f...aluation-order

    Considering "if you read a variable twice in an expression where you also write it, the result is undefined", it means that the expression is also undefined for the pre-increment case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Ah, I wasn't aware of that. *makes mental note for his compiler*

    I suppose that makes sense though, but it seems sites like msdn have just gone and created their own order of operations to define those types of equations. And it appears Stroustrup probably took this ambiguity in to account when he put that up there, judging by the ambiguity witnessed here.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's nothing to do with order of operation (or how many parentheses you use for that matter).

    http://www.eskimo.com/~scs/C-faq/s3.html
    If you attempt to modify the same object more than once between sequence points, the result is undefined - PERIOD.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. preincrement and postincrement operator overloading problem
    By Mathsniper in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2006, 07:57 AM