Macro expansion in C++ preprocessor

B

borophyll

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
 
N

Nera

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.
 
B

borophyll

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

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
 
N

Nera

On Aug 22, 2:39 pm, (e-mail address removed) wrote:
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.
 
B

borophyll

On Aug 22, 2:39 pm, (e-mail address 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
 
N

Neelesh Bodas

On Aug 22, 5:41 pm, (e-mail address removed) wrote:
On Aug 22, 2:39 pm, (e-mail address 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.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top