incrementating integers

This is a discussion on incrementating integers within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; #define SQR(x) ((x) * (x)) int main() { int num=1; cout << SQR(++num) << ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    6

    incrementating integers

    Code:
    #include <iostream>
    using namespace std;
    
    #define SQR(x) ((x) * (x))
    
    int main()
    {
    int num=1;
    
    cout << SQR(++num) << endl ;
    return 0;
    }
    Can you explain how the output is: 9 ?
    I could have thought it would be 1 or if num is incremented by 1 then 4 but how is it 9?

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Very cute.

    The preprocessor replaces
    SQR(++num)
    with
    ((++num) * (++num))

    When the program runs, the outer expression
    ((++num) * (++num))
    can't be evaluated until after both of the inner expressions are evaluated. I don't know which one goes first (maybe C++ leaves this up to the compiler to decide), but either way, first one of the (++num)'s increments num to 2, then the other one increments num to 3, then finally the multiplication is done.
    num * num = 9

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Yet another example of why #define macros shouldn't be used when functions will do.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    ahh cheers, that makes sense,

    I forgot about the preprocessing part!
    thanks

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    yeh joshdick but this was in a revision quiz and was the only question to "make me think". I totally forgot about how preprocessing worked

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    I just wanted to point out that this behavior is undefined. It could produce 1, 2, 3, 4, 6, 9, or 572749.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    could you expand on what you mean by undefined and your example?

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    The variable 'x' is modified twice between sequence points. The C++ standard places no requirements on what happens under such circumstances. There's a very good chance that running the code on different compilers will produce a variety of different results.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by kernal32
    yeh joshdick but this was in a revision quiz and was the only question to "make me think". I totally forgot about how preprocessing worked
    My compiler outputs 6. I hope the revision quiz gave the answer: undefined.

    And it really doesn't matter that it's a macro. This produces the same result:
    cout << ++num * ++num << endl;

  10. #10
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    gcc on linux gives me 9, thats the compiler we are using and the quiz perhaps assumes this, althought we could infact compile it and and give the answer.

    The only valid answer was 9....

    I have a written exam on this tomorrow, I'll put a note that it depends on the compiler then give an answer on what I think gcc would give.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>The only valid answer was 9....
    No it ain't. The only valid answer is "undefined", but I'm sure you've realised this already. Exam questions expecting a specific number as the answer (like 9), are wrong, plain and simple. The term "undefined" means that anything could happen, which means the program isn't even guaranteed to attempt the calculation at all.

    If you get stuck, or just fancy an argument with your teacher, point them to the C++ standard:

    ISO/IEC 14882:1998(E)

    5 Expressions

    4 Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual
    expressions, and the order in which side effects take place, is unspecified. Between the previous
    and next sequence point a scalar object shall have its stored value modified at most once by the evaluation
    of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
    The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full
    expression; otherwise the behavior is undefined. [Example:
    Code:
    i = v[i++]; // the behavior is unspecified
    i = 7, i++, i++; // i becomes 9
    i = ++i + 1; // the behavior is unspecified
    i = i + 1; // the value of i is incremented
    —end example]
    You shouldn't be using #define's for that type of thing in C++ anyway. inline functions will do a much better job. #define's are more prominent in C, avoid them in C++ where you can.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Nov 2003
    Posts
    6
    Hammer, I understand.
    I meant however that in the multichoice quiz the only "Accepted" answer was 9. There was no undefined option

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by kernal32
    Hammer, I understand.
    I meant however that in the multichoice quiz the only "Accepted" answer was 9. There was no undefined option
    Then highlight this as a mistake to your instructor.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. Scanning integers from a file:
    By xarmenx in forum C Programming
    Replies: 6
    Last Post: 11-29-2004, 03:57 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

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