Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Macro expansion in C++ preprocessor

Reply
Thread Tools

Macro expansion in C++ preprocessor

 
 
borophyll@gmail.com
Guest
Posts: n/a
 
      08-22-2007
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

 
Reply With Quote
 
 
 
 
Nera
Guest
Posts: n/a
 
      08-22-2007
On Aug 22, 2:39 pm, (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
borophyll@gmail.com
Guest
Posts: n/a
 
      08-22-2007
On Aug 22, 5:51 pm, Nera <(E-Mail Removed)> wrote:
> On Aug 22, 2:39 pm, (E-Mail Removed) 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

 
Reply With Quote
 
Nera
Guest
Posts: n/a
 
      08-22-2007
On Aug 22, 5:41 pm, (E-Mail Removed) wrote:
> On Aug 22, 5:51 pm, Nera <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Aug 22, 2:39 pm, (E-Mail Removed) 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.

 
Reply With Quote
 
borophyll@gmail.com
Guest
Posts: n/a
 
      08-22-2007
On Aug 22, 8:23 pm, Nera <(E-Mail Removed)> wrote:
> On Aug 22, 5:41 pm, (E-Mail Removed) wrote:
>
>
>
> > On Aug 22, 5:51 pm, Nera <(E-Mail Removed)> wrote:

>
> > > On Aug 22, 2:39 pm, (E-Mail Removed) 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

 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      08-22-2007
On Aug 22, 3:46 pm, (E-Mail Removed) wrote:
> On Aug 22, 8:23 pm, Nera <(E-Mail Removed)> wrote:
>
>
>
> > On Aug 22, 5:41 pm, (E-Mail Removed) wrote:

>
> > > On Aug 22, 5:51 pm, Nera <(E-Mail Removed)> wrote:

>
> > > > On Aug 22, 2:39 pm, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
Problem with macro expansion me C++ 1 11-09-2004 02:38 PM
Macro expansion: intercept statement interpretation Benjamin Niemann Python 3 08-26-2004 02:51 AM
selective preprocessor expansion max(01)* C Programming 6 08-12-2004 08:41 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM



Advertisments