preincrement

This is a discussion on preincrement within the C++ Programming forums, part of the General Programming Boards category; Hello Code: #include <iostream.h> int main() { int c=3; cout << (((++c)) + ((++c))) << endl; return 0; } The ...

  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
    University of Waterloo
    Posts
    1,903
    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
    21,739
    The expression is ambiguous.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    University of Waterloo
    Posts
    1,903
    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
    21,739
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    University of Waterloo
    Posts
    1,903
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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

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