Preprocessor parsing rules

C

claus.tondering

Consider the following (weird) preprocessor code:

#define IDENT(x) x
#define ALPHA(x) BETA IDENT((x))
#define BETA(x) (x+2)

ALPHA(5)

Running this through Microsofts C preprocessor produces this result:

(5+2)

which is what I would expect. But if I run the same input through GCC's
preprocessor, I get

BETA (5)

which surprises me. (I'm using GCC version 3.4.4.)

Shouldn't the preprocessor rescan the line and replace the BETA macro?
Is this therefore a bug in GCC, or have I misunderstood something (in
which case Microsoft's compiler is doing something wrong)?
 
R

Robert Gamble

Consider the following (weird) preprocessor code:

#define IDENT(x) x
#define ALPHA(x) BETA IDENT((x))
#define BETA(x) (x+2)

ALPHA(5)

Running this through Microsofts C preprocessor produces this result:

(5+2)

which is what I would expect. But if I run the same input through GCC's
preprocessor, I get

BETA (5)

which surprises me. (I'm using GCC version 3.4.4.)

Shouldn't the preprocessor rescan the line and replace the BETA macro?
Is this therefore a bug in GCC, or have I misunderstood something (in
which case Microsoft's compiler is doing something wrong)?

You are defining BETA as a function-like macro but invoking it as an
object-like macro. I believe ggc is correct here. You will get the
results you want if you change the definition of ALPHA:
#define ALPHA(x) BETA(IDENT((x)))

Robert Gamble
 
S

S.Tobias

Consider the following (weird) preprocessor code:

#define IDENT(x) x
#define ALPHA(x) BETA IDENT((x))
#define BETA(x) (x+2)

ALPHA(5)

Running this through Microsofts C preprocessor produces this result:

(5+2)

which is what I would expect. But if I run the same input through GCC's
preprocessor, I get

BETA (5)

which surprises me. (I'm using GCC version 3.4.4.)

Shouldn't the preprocessor rescan the line and replace the BETA macro?

No, it has already rescanned `BETA'. Gcc is right, MS compiler is wrong.

ALPHA(5)
BETA IDENT((5))
^--rescan from here
BETA is not followed by a `(', not a macro call, go on...
IDENT is followed by `(', expand
BETA (5)
^--rescan from here
No macro calls found
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top