Thread: What different in macros?

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    What different in macros?

    Code:
    #define max(x,y) (x)>(y)?(x):(y)
    .........
    .............
    .........
    int a=5,b=3;
    int k=max(a++,++b);
    cout<<k;
    ...............
    if it's gonna be a function the call is like:
    Code:
    max(5,4)
    but when its, macro how the call ll be and how the proccesing will be?

    a detailed explanation pls

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perform the macro expansion yourself then reason about the resulting code:
    Code:
    int a=5,b=3;
    int k=(a++)>(++b)?(a++):(++b);
    cout<<k;
    Consequently, it would be better to use an inline function template instead of a macro.
    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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    Quote Originally Posted by laserlight View Post
    Perform the macro expansion yourself then reason about the resulting code:
    Code:
    int a=5,b=3;
    int k=(a++)>(++b)?(a++):(++b);
    cout<<k;
    Consequently, it would be better to use an inline function template instead of a macro.
    yes i know.but the answer is different from what we have in function...
    The called number is not 5,4 basically

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    take for example:
    Code:
    #define MAN(x,y) (x)>(y)?(x):(y)
    .............
     int i=10,j=5,k=0;
     k= MAN(i++,++j);
     printf("%d %d %d",i,j,k);
    i thought the answer would be: 11,6,10
    but the answer is 12,6,11
    how is it

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    /dev/pts/0
    Posts
    29
    . . . think about it for one moment, here. laserlight has suggested you expand it out yourself, which s/he has done kindly for you.

    How many times is a going to be incremented? How about b?
    -- strange

    There is no Darkness in Eternity
    Only Light too dim for us to see

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    yes i know.but the answer is different from what we have in function...
    If you know, then there should be no "but"

    There is a sequence point at the '?'. So, the post-increment would have been effective by the time control reaches the second or third operands of the ternary operator, whereas with a function call the first argument would have the original value of a.

    Quote Originally Posted by dpp
    The called number is not 5,4 basically
    What do you mean by "called number"? There is no function call.
    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

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    yeah thanks

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's exactly why macros are evil and should be avoided whenever possible.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What laserlight and cpjust are pointing out:

    Post and pre incrementing on the same line bring out demons in your code, especially if macros are used in tandem with increment operators. Your macro will just expand this out. Troubleshooting these are a pain as well. Caution is very much needed when you define those. I have done something similar to the following:

    Code:
    /* Some universal defines to aid in swapping out two keys of int types */
    typedef int Item;      /*can easily be modified as necessary */
    #define key(A)  (A)
    #define less(A, B) (key(A) < key(B))
    #define exch(A, B) {Item t = A; A = B; B = t;}
    
    
    /* more defines built from above definitions */
    #define compexch(A, B) if (less(B, A)) exch(A, B)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. function definition with macros
    By toshog in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2009, 09:22 PM
  3. Pre-processor macros causing segfaults
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2009, 02:35 AM
  4. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  5. Visual C++ and UNICODE - _t macros
    By nvoigt in forum Windows Programming
    Replies: 2
    Last Post: 04-22-2005, 07:42 AM