use of #define MACRO(A,B)

Discussion in 'C Programming' started by Tagore, Mar 31, 2010.

  1. Tagore

    Tagore Guest

    hi,

    While understanding some code, I came across a definition of macro
    without expansion
    #define MACRO(A,B) // expansion missing

    What could be use of such macro?

    I think, there is no use of it without expanding e.g.
    #define MACRO(A,B) A+B // expansion given

    Please clear doubts

    Thanks,
    Tagore, Mar 31, 2010
    #1
    1. Advertising

  2. Tagore <> wrote:
    > While understanding some code, I came across a definition
    > of macro without expansion
    > #define MACRO(A,B)      // expansion missing
    >
    > What could be use of such macro?


    To produce nothing in certain circumstances.

    > I think, there is no use of it without expanding e.g.
    > #define MACRO(A,B) A+B    // expansion given
    >
    > Please clear doubts


    #if NDEBUG
    #define DEBUG(a,b)
    #else
    #define DEBUG(a,b) fprintf(stderr, "%s: %d\n", a, (int) (b))
    #endif

    DEBUG("Foo()", 42);

    --
    Peter
    Peter Nilsson, Mar 31, 2010
    #2
    1. Advertising

  3. On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
    <> wrote:

    >hi,
    >
    >While understanding some code, I came across a definition of macro
    >without expansion
    >#define MACRO(A,B) // expansion missing


    I hope the comment is your addition and not present in the source.

    >
    >What could be use of such macro?
    >
    >I think, there is no use of it without expanding e.g.
    >#define MACRO(A,B) A+B // expansion given
    >
    >Please clear doubts


    At one time early in the development, it could have been a debugging
    macro of the form
    #define MACRO(A,B) \
    printf(#A "=%d, " #B "=%d.\n", A, B)

    After the developer was satisfied, the new definition reduces any
    invocations of MACO to white space which should be ignored during
    compilation.

    --
    Remove del for email
    Barry Schwarz, Mar 31, 2010
    #3
  4. Tagore

    Seebs Guest

    On 2010-03-31, Tagore <> wrote:
    > While understanding some code, I came across a definition of macro
    > without expansion
    > #define MACRO(A,B) // expansion missing


    > What could be use of such macro?


    To make calls of the form "MACRO(arg1, arg2)" turn into nothing.

    For instance, consider

    #ifdef VERBOSE
    #define DEBUG(x) x
    #else
    #define DEBUG(x)
    #endif

    DEBUG(printf("%d\n", foo->bar));

    If VERBOSE is defined, this expands to printf, otherwise to an empty
    statement which does nothing.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Mar 31, 2010
    #4
  5. Tagore

    Thad Smith Guest

    Barry Schwarz wrote:
    > On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
    > <> wrote:


    >> While understanding some code, I came across a definition of macro
    >> without expansion
    >> #define MACRO(A,B) // expansion missing

    >
    > I hope the comment is your addition and not present in the source.


    While I typically don't use // comments, I have otherwise written macros with a
    commented whitespace-only body, perhaps
    #define MACRO(A,B) /* nothing */

    It documented that the macro replacement was intentionally empty.

    --
    Thad
    Thad Smith, Mar 31, 2010
    #5
  6. On Wed, 31 Mar 2010 06:35:25 -0700, Thad Smith <>
    wrote:

    >Barry Schwarz wrote:
    >> On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
    >> <> wrote:

    >
    >>> While understanding some code, I came across a definition of macro
    >>> without expansion
    >>> #define MACRO(A,B) // expansion missing

    >>
    >> I hope the comment is your addition and not present in the source.

    >
    >While I typically don't use // comments, I have otherwise written macros with a
    >commented whitespace-only body, perhaps
    >#define MACRO(A,B) /* nothing */
    >
    >It documented that the macro replacement was intentionally empty.


    But your comment is closed, so a statement of the form
    if (x)
    MACRO(y,z);
    compiles as
    if (x)
    /* nothing */ ;
    while the code in the OP will compile as
    if (x)
    // expansion missing ;
    which will do something unintended with the next statement.

    --
    Remove del for email
    Barry Schwarz, Apr 1, 2010
    #6
  7. Barry Schwarz <> writes:

    > On Wed, 31 Mar 2010 06:35:25 -0700, Thad Smith <>
    > wrote:
    >
    >>Barry Schwarz wrote:
    >>> On Tue, 30 Mar 2010 17:55:32 -0700 (PDT), Tagore
    >>> <> wrote:

    >>
    >>>> While understanding some code, I came across a definition of macro
    >>>> without expansion
    >>>> #define MACRO(A,B) // expansion missing
    >>>
    >>> I hope the comment is your addition and not present in the source.

    >>
    >>While I typically don't use // comments, I have otherwise written macros with a
    >>commented whitespace-only body, perhaps
    >>#define MACRO(A,B) /* nothing */
    >>
    >>It documented that the macro replacement was intentionally empty.

    >
    > But your comment is closed, so a statement of the form
    > if (x)
    > MACRO(y,z);
    > compiles as
    > if (x)
    > /* nothing */ ;
    > while the code in the OP will compile as
    > if (x)
    > // expansion missing ;
    > which will do something unintended with the next statement.


    This is not right. Comments are removed (well, replaced with a single
    space) in translation phase 3 before pre-processing is done. Both
    definitions of MACRO are empty.

    --
    Ben.
    Ben Bacarisse, Apr 1, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,111
    John Harrison
    Jul 14, 2004
  2. Peng Yu
    Replies:
    6
    Views:
    6,433
    Method Man
    Oct 4, 2004
  3. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    579
    Jack Klein
    Sep 21, 2003
  4. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    501
    Mark McIntyre
    Nov 22, 2005
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    470
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page