A question on macro expansion

Discussion in 'C Programming' started by Ark, Jul 22, 2004.

  1. Ark

    Ark Guest

    Hello, NG,
    Please, help on this snippet:

    #define CAT(a,b) a##b
    #define COMMENT CAT(/,/)
    COMMENT This is a comment

    Should it compile?
    It passes MS C/C++ 13.0 (Visual Studio 2002) and fails IAR ARM
    compiler.
    IAR claims that comments should not be processed after macro
    expansion, so I have an odd situation where the preprocessor output is
    as intended and is compilable, whereas the original file is not.

    What is the truth, according to the standard(s)?
    (I myself can read but cannot parse 'em :)

    Thank you very much,
    Ark
    Ark, Jul 22, 2004
    #1
    1. Advertising

  2. Ark

    Eric Sosman Guest

    Ark wrote:
    > Hello, NG,
    > Please, help on this snippet:
    >
    > #define CAT(a,b) a##b
    > #define COMMENT CAT(/,/)
    > COMMENT This is a comment
    >
    > Should it compile?


    No.

    > It passes MS C/C++ 13.0 (Visual Studio 2002) and fails IAR ARM
    > compiler.
    > IAR claims that comments should not be processed after macro
    > expansion, so I have an odd situation where the preprocessor output is
    > as intended and is compilable, whereas the original file is not.
    >
    > What is the truth, according to the standard(s)?
    > (I myself can read but cannot parse 'em :)


    Comments are removed in translation phase 3. Macros
    are expanded in phase 4.

    "The preprocessor output" is really just a sort of
    courtesy many compilers extend. The Standard does not
    require that such output be obtainable, and certainly
    doesn't require that the output (if any) have the same
    meaning as the code from which it was produced. This
    is a case of the latter situation: The preprocessed
    output is valid C, but the source from which it was
    generated is not.

    --
    Eric Sosman, Jul 22, 2004
    #2
    1. Advertising

  3. Ark

    jacob navia Guest

    This should not compile according to the standard.
    According to Microsoft yes.

    Problem is that windows header files use this feature to
    selectiveley comment out portions of the code
    according to macro definitions embedded in
    #ifdefs, well the usual stuff.

    This was one of the many problems with those headers.
    They tend to be very Microsoft specific.
    jacob navia, Jul 22, 2004
    #3
  4. Ark

    ark Guest

    "jacob navia" <> wrote in message
    news:cdp3s1$e9d$...
    > This should not compile according to the standard.
    > According to Microsoft yes.
    >
    > Problem is that windows header files use this feature to
    > selectiveley comment out portions of the code
    > according to macro definitions embedded in
    > #ifdefs, well the usual stuff.
    >
    > This was one of the many problems with those headers.
    > They tend to be very Microsoft specific.
    >
    >

    While we are at it...
    #define CAT(a,b) a##b
    #define CATB(a,b,c) CAT(CAT(a,b),c)
    #define CATA(a,b,c) CAT(a,CAT(b,c))
    Should I expect that CATB(pro, duct, ion) expand to production, and same for
    CATA ?
    Thanks,
    - Ark


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.722 / Virus Database: 478 - Release Date: 7/21/2004
    ark, Jul 23, 2004
    #4
    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. me
    Replies:
    1
    Views:
    1,121
    Victor Bazarov
    Nov 9, 2004
  2. Benjamin Niemann
    Replies:
    3
    Views:
    325
    Caleb Hattingh
    Aug 26, 2004
  3. Vittal

    Macro Expansion

    Vittal, Mar 22, 2005, in forum: C Programming
    Replies:
    3
    Views:
    406
    Eric Sosman
    Mar 23, 2005
  4. Dom Gilligan

    Macro expansion of '#__LINE__'?

    Dom Gilligan, Nov 4, 2005, in forum: C Programming
    Replies:
    4
    Views:
    449
    Dom Gilligan
    Nov 4, 2005
  5. reppisch

    partial/selective Macro expansion scpp

    reppisch, May 5, 2006, in forum: C Programming
    Replies:
    7
    Views:
    368
    michi
    May 7, 2006
Loading...

Share This Page