Macro invocation

Discussion in 'C Programming' started by Ron, Dec 1, 2003.

  1. Ron

    Ron Guest

    In macro invocation, what kind of tokens can I pass as arguments?

    Another question: can I pass more than one token for single arguments? eg.

    #define MYMACRO(arg1, arg2) arg1 arg2

    MYMACRO("abc" L'd' .12, "def")

    Is it valid? But if I can pass anything I could even pass a comma since
    it's a preprocessing token, ok? So i could write:

    MYMACRO("abc" , L'd' .12, "def")

    That is ambiguous since it's not clear if the first comma closes the first
    argument or if it's just another token to pass. Please, give me an answer.

    Greetings.

    ps: i'm supposing that c and c++ preprocessors follow almost the same rules,
    but not sure it's right.
     
    Ron, Dec 1, 2003
    #1
    1. Advertisements

  2. in comp.lang.c i read:
    short answer: no.
    the first argument consists of three tokens of incompatible type, so there
    is no way to stitch them together, hence are not acceptable syntactically.

    there are two arguments, because the , is the argument separation token:

    [#11] The sequence of preprocessing tokens bounded by the
    outside-most matching parentheses forms the list of arguments for
    the function-like macro. The individual arguments within the list
    are separated by comma preprocessing tokens, but comma preprocessing
    tokens between matching inner parentheses do not separate arguments.
    If there are sequences of preprocessing tokens within the list of
    arguments that would otherwise act as preprocessing directives, the
    behavior is undefined.
    to pass a comma as an argument it would need to be within an additional set
    of parentheses. (which still won't work because the tokens present cannot
    be stitched together.)
    your post is to comp.lang.c and so you shall get a c language response,
    irrespective of whether it applies to c++.
     
    those who know me have no need of my name, Dec 1, 2003
    #2
    1. Advertisements

  3. Ron

    Dan Pop Guest

    It's also the wrong answer. Multitoken arguments are just fine as single
    macro arguments:

    size_t offset = offsetof(struct tm, tm_sec);

    Dan
     
    Dan Pop, Dec 2, 2003
    #3
  4. in comp.lang.c i read:
    you are quite correct. i can clearly see how i managed to write what i
    wrote, yet i have no idea how that much brain damage found it's way onto
    the net.

    sorry Ron, Dan is quite correct -- you can indeed provide a macro argument
    with multiple tokens. e.g., Dan's example of:
    works swimmingly, in that when the first argument is expanded the resulting
    `struct tm' can be usefully used in quite a number of contexts, including
    the specific one cited. arbitrary stuff such as you used as an example:

    will expand fine, but later processing will likely have a syntax error,
    there being no context (i can imagine) where a string, character and
    floating point literal can be used without intervening operators and/or
    enclosing functions.
     
    those who know me have no need of my name, Dec 2, 2003
    #4
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.