Thread: Macro Confusion

  1. #1
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22

    Macro Confusion

    The output is 9 49 can any1 explain how we get this output?
    Code:
    #define P(x) (x*x)
    main( )
    {
    
    int i=3,j,k;
    j=P(i++);
    k=P(++i);
    printf("\n%d %d",j,k);
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    i is 3, so the first one is simple: 3 * 3 is 9. x is replaced with i++, so after j=P(i++) i is 5.

    The second one i is incremented twice, so k=P(++i) is 7 * 7 which is 49.

    But I'm pretty sure this is undefined behaviour, so it would only explain what may have happened in this case.

  3. #3
    Registered User Waleed Mujeeb's Avatar
    Join Date
    Jan 2012
    Posts
    22
    Thansk i got it. The difference lies in post and pre increment operators.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    But you can make no assumptions about it in this case. For example, I tried this:

    Code:
    printf("%d %d\n", (i++ * i++), (++i * ++i) );
    And clang gave me: 12, 42
    but gcc: 25, 25

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The difference is not only in pre and post increment operators. The issue is that i is being modified multiple times without intervening sequence points. That is undefined behaviour.

    If both lines computed a value of 42, the behaviour would still be correct. If the code reformatted your hard drive, the behaviour would still be correct. That is what "undefined" means in the C standard: any behaviour is allowed, and every possible behaviour is equally correct.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > The output is 9 49 can any1 explain how we get this output?
    Sure -> Comma Operator - C And C++ | Dream.In.Code

    You're just not going to get a consistent answer out of code like that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. macro
    By cable in forum C Programming
    Replies: 6
    Last Post: 09-17-2011, 06:54 AM
  2. What does following macro mean?
    By sanddune008 in forum C Programming
    Replies: 5
    Last Post: 08-27-2010, 06:27 PM
  3. Macro confusion
    By al_engler in forum C Programming
    Replies: 8
    Last Post: 12-31-2006, 07:45 AM
  4. macro's
    By Steven Snell in forum C Programming
    Replies: 3
    Last Post: 10-14-2002, 12:04 AM