Macro expansion in C++ preprocessor

Discussion in 'C++' started by borophyll@gmail.com, Aug 22, 2007.

  1. Guest

    Hi all

    Can anyone explain to me the algorithm for macro expansion in the C++
    preprocessor. I am confused why the following code works like it
    does:

    #define A(x) #x
    #define B(x) A(x)
    #define TEST 1


    A(TEST)
    B(TEST)

    When I run the preprocessor, It gives me "TEST" and "1". Can anyone
    explain? I thought they would be the same....

    regards, B
     
    , Aug 22, 2007
    #1
    1. Advertising

  2. Nera Guest

    On Aug 22, 2:39 pm, wrote:
    > Hi all
    >
    > Can anyone explain to me the algorithm for macro expansion in the C++
    > preprocessor. I am confused why the following code works like it
    > does:
    >
    > #define A(x) #x
    > #define B(x) A(x)
    > #define TEST 1
    >
    > A(TEST)
    > B(TEST)
    >
    > When I run the preprocessor, It gives me "TEST" and "1". Can anyone
    > explain? I thought they would be the same....
    >
    > regards, B


    According to C Standard:
    [6.10.3.1.1]
    After the arguments for the invocation of a function-like macro have
    been
    identified, argument substitution takes place. A parameter in the
    replacement list, unless preceded by a # or ## preprocessing token or
    followed by a ## preprocessing token (see below), is replaced by the
    corresponding argument after all macros contained therein have been
    expanded. Before being substituted, each argument's preprocessing
    tokens
    are completely macro replaced as if they formed the rest of the
    preprocessing file; no other preprocessing tokens are available.
    So '#' make the differences.
     
    Nera, Aug 22, 2007
    #2
    1. Advertising

  3. Guest

    On Aug 22, 5:51 pm, Nera <> wrote:
    > On Aug 22, 2:39 pm, wrote:
    >
    >
    >
    > > Hi all

    >
    > > Can anyone explain to me the algorithm for macro expansion in the C++
    > > preprocessor. I am confused why the following code works like it
    > > does:

    >
    > > #define A(x) #x
    > > #define B(x) A(x)
    > > #define TEST 1

    >
    > > A(TEST)
    > > B(TEST)

    >
    > > When I run the preprocessor, It gives me "TEST" and "1". Can anyone
    > > explain? I thought they would be the same....

    >
    > > regards, B

    >
    > According to C Standard:
    > [6.10.3.1.1]
    > After the arguments for the invocation of a function-like macro have
    > been
    > identified, argument substitution takes place. A parameter in the
    > replacement list, unless preceded by a # or ## preprocessing token or
    > followed by a ## preprocessing token (see below), is replaced by the
    > corresponding argument after all macros contained therein have been
    > expanded. Before being substituted, each argument's preprocessing
    > tokens
    > are completely macro replaced as if they formed the rest of the
    > preprocessing file; no other preprocessing tokens are available.
    > So '#' make the differences.


    I understand that # is what makes the result of A(TEST) come to be
    "TEST", its the outcome of B(TEST) that I don't understand. Shouldn't
    the preprocessor perform the following steps:

    Expand the macro for B(TEST) ---> gives us A(TEST)
    Expand the macro for A(TEST) ---> gives us #TEST
    Now, do not expand TEST, since it is preceded by #
    Applying # operator ---> gives us "TEST"

    I need to know how and why the macro TEST is expanded in the second
    version, and not the first version

    regards, B
     
    , Aug 22, 2007
    #3
  4. Nera Guest

    On Aug 22, 5:41 pm, wrote:
    > On Aug 22, 5:51 pm, Nera <> wrote:
    >
    >
    >
    >
    >
    > > On Aug 22, 2:39 pm, wrote:

    >
    > > > Hi all

    >
    > > > Can anyone explain to me the algorithm for macro expansion in the C++
    > > > preprocessor. I am confused why the following code works like it
    > > > does:

    >
    > > > #define A(x) #x
    > > > #define B(x) A(x)
    > > > #define TEST 1

    >
    > > > A(TEST)
    > > > B(TEST)

    >
    > > > When I run the preprocessor, It gives me "TEST" and "1". Can anyone
    > > > explain? I thought they would be the same....

    >
    > > > regards, B

    >
    > > According to C Standard:
    > > [6.10.3.1.1]
    > > After the arguments for the invocation of a function-like macro have
    > > been
    > > identified, argument substitution takes place. A parameter in the
    > > replacement list, unless preceded by a # or ## preprocessing token or
    > > followed by a ## preprocessing token (see below), is replaced by the
    > > corresponding argument after all macros contained therein have been
    > > expanded. Before being substituted, each argument's preprocessing
    > > tokens
    > > are completely macro replaced as if they formed the rest of the
    > > preprocessing file; no other preprocessing tokens are available.
    > > So '#' make the differences.

    >
    > I understand that # is what makes the result of A(TEST) come to be
    > "TEST", its the outcome of B(TEST) that I don't understand. Shouldn't
    > the preprocessor perform the following steps:
    >
    > Expand the macro for B(TEST) ---> gives us A(TEST)
    > Expand the macro for A(TEST) ---> gives us #TEST
    > Now, do not expand TEST, since it is preceded by #
    > Applying # operator ---> gives us "TEST"
    >
    > I need to know how and why the macro TEST is expanded in the second
    > version, and not the first version
    >
    > regards, B- Hide quoted text -
    >
    > - Show quoted text -


    Read this sentence:
    A parameter in the replacement list, unless preceded by a # or ##
    preprocessing token or followed by a ## preprocessing token (see
    below), is replaced by the corresponding argument after all macros
    contained therein have been expanded.

    This exception does not apply to B(TEST),it may expand it this way:
    B(TEST) --> B(1) -->A(1) -->#1 -->"1".
    And i think this better be posted in comp.lang.c.

    BR.
     
    Nera, Aug 22, 2007
    #4
  5. Guest

    On Aug 22, 8:23 pm, Nera <> wrote:
    > On Aug 22, 5:41 pm, wrote:
    >
    >
    >
    > > On Aug 22, 5:51 pm, Nera <> wrote:

    >
    > > > On Aug 22, 2:39 pm, wrote:

    >
    > > > > Hi all

    >
    > > > > Can anyone explain to me the algorithm for macro expansion in the C++
    > > > > preprocessor. I am confused why the following code works like it
    > > > > does:

    >
    > > > > #define A(x) #x
    > > > > #define B(x) A(x)
    > > > > #define TEST 1

    >
    > > > > A(TEST)
    > > > > B(TEST)

    >
    > > > > When I run the preprocessor, It gives me "TEST" and "1". Can anyone
    > > > > explain? I thought they would be the same....

    >
    > > > > regards, B

    >
    > > > According to C Standard:
    > > > [6.10.3.1.1]
    > > > After the arguments for the invocation of a function-like macro have
    > > > been
    > > > identified, argument substitution takes place. A parameter in the
    > > > replacement list, unless preceded by a # or ## preprocessing token or
    > > > followed by a ## preprocessing token (see below), is replaced by the
    > > > corresponding argument after all macros contained therein have been
    > > > expanded. Before being substituted, each argument's preprocessing
    > > > tokens
    > > > are completely macro replaced as if they formed the rest of the
    > > > preprocessing file; no other preprocessing tokens are available.
    > > > So '#' make the differences.

    >
    > > I understand that # is what makes the result of A(TEST) come to be
    > > "TEST", its the outcome of B(TEST) that I don't understand. Shouldn't
    > > the preprocessor perform the following steps:

    >
    > > Expand the macro for B(TEST) ---> gives us A(TEST)
    > > Expand the macro for A(TEST) ---> gives us #TEST
    > > Now, do not expand TEST, since it is preceded by #
    > > Applying # operator ---> gives us "TEST"

    >
    > > I need to know how and why the macro TEST is expanded in the second
    > > version, and not the first version

    >
    > > regards, B- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Read this sentence:
    > A parameter in the replacement list, unless preceded by a # or ##
    > preprocessing token or followed by a ## preprocessing token (see
    > below), is replaced by the corresponding argument after all macros
    > contained therein have been expanded.
    >
    > This exception does not apply to B(TEST),it may expand it this way:
    > B(TEST) --> B(1) -->A(1) -->#1 -->"1".
    > And i think this better be posted in comp.lang.c.
    >
    > BR.


    So, why cannot the preprocessor do this?:

    A(TEST) --> A(1) --> #1 --> "1"

    I don't see the difference??? I really am confused

    regards, B
     
    , Aug 22, 2007
    #5
  6. On Aug 22, 3:46 pm, wrote:
    > On Aug 22, 8:23 pm, Nera <> wrote:
    >
    >
    >
    > > On Aug 22, 5:41 pm, wrote:

    >
    > > > On Aug 22, 5:51 pm, Nera <> wrote:

    >
    > > > > On Aug 22, 2:39 pm, wrote:

    >
    > > > > > Hi all

    >
    > > > > > Can anyone explain to me the algorithm for macro expansion in the C++
    > > > > > preprocessor. I am confused why the following code works like it
    > > > > > does:

    >
    > > > > > #define A(x) #x
    > > > > > #define B(x) A(x)
    > > > > > #define TEST 1

    >
    > > > > > A(TEST)
    > > > > > B(TEST)

    >
    > > > > > When I run the preprocessor, It gives me "TEST" and "1". Can anyone
    > > > > > explain? I thought they would be the same....

    >
    > > > > > regards, B

    >
    > > > > According to C Standard:
    > > > > [6.10.3.1.1]
    > > > > After the arguments for the invocation of a function-like macro have
    > > > > been
    > > > > identified, argument substitution takes place. A parameter in the
    > > > > replacement list, unless preceded by a # or ## preprocessing token or
    > > > > followed by a ## preprocessing token (see below), is replaced by the
    > > > > corresponding argument after all macros contained therein have been
    > > > > expanded. Before being substituted, each argument's preprocessing
    > > > > tokens
    > > > > are completely macro replaced as if they formed the rest of the
    > > > > preprocessing file; no other preprocessing tokens are available.
    > > > > So '#' make the differences.

    >
    > > > I understand that # is what makes the result of A(TEST) come to be
    > > > "TEST", its the outcome of B(TEST) that I don't understand. Shouldn't
    > > > the preprocessor perform the following steps:

    >
    > > > Expand the macro for B(TEST) ---> gives us A(TEST)
    > > > Expand the macro for A(TEST) ---> gives us #TEST
    > > > Now, do not expand TEST, since it is preceded by #
    > > > Applying # operator ---> gives us "TEST"

    >
    > > > I need to know how and why the macro TEST is expanded in the second
    > > > version, and not the first version

    >
    > > > regards, B- Hide quoted text -

    >
    > > > - Show quoted text -

    >
    > > Read this sentence:
    > > A parameter in the replacement list, unless preceded by a # or ##
    > > preprocessing token or followed by a ## preprocessing token (see
    > > below), is replaced by the corresponding argument after all macros
    > > contained therein have been expanded.

    >
    > > This exception does not apply to B(TEST),it may expand it this way:
    > > B(TEST) --> B(1) -->A(1) -->#1 -->"1".
    > > And i think this better be posted in comp.lang.c.

    >
    > > BR.

    >
    > So, why cannot the preprocessor do this?:
    >
    > A(TEST) --> A(1) --> #1 --> "1"
    >
    > I don't see the difference??? I really am confused
    >


    B(TEST) --> A(1) ---> #1 ---> "1"
    //here macro replacement for B and TEST takes place, and then
    replacement for A will take place

    A(TEST) ---> #TEST --> "TEST"
    //here TEST comes immediately after #, hence it won't undergo any
    replacement

    -N
     
    Neelesh Bodas, Aug 22, 2007
    #6
    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,134
    Victor Bazarov
    Nov 9, 2004
  2. Benjamin Niemann
    Replies:
    3
    Views:
    335
    Caleb Hattingh
    Aug 26, 2004
  3. Ark

    A question on macro expansion

    Ark, Jul 22, 2004, in forum: C Programming
    Replies:
    3
    Views:
    360
  4. max(01)*

    selective preprocessor expansion

    max(01)*, Aug 11, 2004, in forum: C Programming
    Replies:
    6
    Views:
    940
    Emmanuel Delahaye
    Aug 12, 2004
  5. Vittal

    Macro Expansion

    Vittal, Mar 22, 2005, in forum: C Programming
    Replies:
    3
    Views:
    416
    Eric Sosman
    Mar 23, 2005
Loading...

Share This Page