Thread: MACRO question

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    7

    MACRO question

    Hey guys, I am studying some C from K&R book and I just play around with these. Anyway, I have a question.

    Lets say this is a macro I have gotten.
    #define ADD(X,Y)(X+Y)

    (I know it will be wiser to code it: #define ADD(X,Y)((X)+(Y)) )

    so when I get:
    int x = 100, y = 3;
    int z = ADD(x,++y);
    printf("x %d y %d z %d\n",x,y,z);
    x = 100; y = 3;
    z = ADD(x+,+y);
    printf("x %d y %d z %d\n",x,y,z);

    why do I get a different results? I mean, the compiler during the pre-processing should replace ADD(x,++y) and ADD(x+,+y) with x+++y. Thus, they should give the same result.

    Thanks in adv.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I believe (though I may be mistaken) that a sequence of three consecutive plusses is undefined because it is ambiguous. The compiler could legitimately interpret x+++y as (x++) + y or x + (++y), or even (x+)+(+y) if it wants to. And it can interpret them differently even within the same program.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheBigH
    I believe (though I may be mistaken) that a sequence of three consecutive plusses is undefined because it is ambiguous. The compiler could legitimately interpret x+++y as (x++) + y or x + (++y), or even (x+)+(+y) if it wants to. And it can interpret them differently even within the same program.
    You're mistaken: greedy parsing is required, so x+++y must be interpreted as x++ + y.
    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
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    OK, I stand corrected. But then why is x not incremented in the second example?

    If I do z=x+++y directly without going through the macro I get x=101, y=3, z=103 as I would expect.
    Last edited by TheBigH; 02-15-2012 at 01:18 AM. Reason: ...tried it directly
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheBigH
    But then why is x not incremented in the second example?
    Testing with gcc 4.4.4, I get:
    Code:
    x 100 y 4 z 104
    x 100 y 3 z 103
    I removed the include and printf statements, and got the compiler to perform preprocessing only. The result:
    Code:
    int main(int argc, char *argv[])
    {
        int x = 100, y = 3;
        int z = (x+ ++y);
        x = 100; y = 3;
        z = (x+ + +y);
        return 0;
    }
    I believe that this line:
    Code:
    z = (x+ + +y);
    is equivalent to:
    Code:
    z = x + (+(+y));
    Hence neither x nor y is changed.
    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
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    Thanks a lot for the help.
    So from what you're saying, during the pre-processing, the compiler separate the two operands?
    I mean: ADD(x++,y) will be x++ + y and not x+++y, even though I haven't mentioned any space between those in the macro?

    In addition, can you explain or give me a link on how to run pre-process (and seeing the results) without compiling, it sounds interesting.

    Thanks in adv.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by baverlyhills2 View Post
    In addition, can you explain or give me a link on how to run pre-process (and seeing the results) without compiling, it sounds interesting.
    I believe the cpp command/program does it.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by baverlyhills2
    So from what you're saying, during the pre-processing, the compiler separate the two operands?
    Yes.

    Quote Originally Posted by baverlyhills2
    In addition, can you explain or give me a link on how to run pre-process (and seeing the results) without compiling, it sounds interesting.
    Refer to your compiler documentation. For example, with gcc, the -E option should be used.
    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

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    Thanks for everything! (YES, I am using GCC on Linux).

    I have another question if you don't mind. It is related to bitwise, though.
    Since char is 8 bits, then it can store only 0xFF to 0x00 numbers.
    Then, why the output is FFFFFFFE when I am compiling and running the code:
    char x = 0x01;
    printf("%x\n",~x);
    (~x) has to be FE, hasn't it?

    Thanks in adv.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Integer promotion happens when you do the ~x.
    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

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    7
    Alright, that's one thing I haven't known (Doing some research on the net). Thanks for that.

    Well, that's all for now. THANKS you all for your time, help and support. I took a lot from your replies, guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Macro question
    By Mole42 in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2011, 04:41 PM
  2. newbie macro question
    By spank in forum C Programming
    Replies: 3
    Last Post: 05-11-2007, 01:59 PM
  3. Macro question
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 06-26-2005, 01:13 PM
  4. _T Macro Question
    By mrafcho001 in forum Windows Programming
    Replies: 9
    Last Post: 06-11-2005, 09:00 AM
  5. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 05:01 PM