How macro is preprocessed

S

September5th

Hi guys:
Why the following two both works???


#include <stdio.h>
#define A B
#define B 7
int main()
{
printf("%d", A); // print 7
return 0;
}



#include <stdio.h>
#define B 7
#define A B
int main()
{
printf("%d", A);
return 0;
}

How does MACRO is preprocessed???
Thanks a lot.
 
E

Eric Sosman

Hi guys:
Why the following two both works???


#include<stdio.h>
#define A B
#define B 7
int main()
{
printf("%d", A); // print 7
return 0;
}



#include<stdio.h>
#define B 7
#define A B
int main()
{
printf("%d", A);
return 0;
}

How does MACRO is preprocessed???

First, in

printf("%d", A);

.... the compiler sees that A is a macro, and replaces the macro
name with the macro's definition:

printf ("%d", B);

B is also a macro, so the compiler again replaces the macro
name with the macro's definition:

printf ("%d", 7);

All macro names have now been replaced (unless printf is a
macro, which is unlikely), so no further substitutions occur.

Macro replacement does not occur in #define directives. So
in the code:

#define B 7
#define A B

.... the macro A has B as its body, not 7. Try this experiment:

#define B 7
#define A B

printf ("%d\n", A);

#undef B
#define B 42

printf ("%d\n", A);

You will see that two different numbers are printed, even though
both printf calls use the same macro A.
 
S

September5th

     First, in

        printf("%d", A);

... the compiler sees that A is a macro, and replaces the macro
name with the macro's definition:

        printf ("%d", B);

     B is also a macro, so the compiler again replaces the macro
name with the macro's definition:

        printf ("%d", 7);

     All macro names have now been replaced (unless printf is a
macro, which is unlikely), so no further substitutions occur.

     Macro replacement does not occur in #define directives.  So
in the code:

        #define B 7
        #define A B

... the macro A has B as its body, not 7.  Try this experiment:

        #define B 7
        #define A B

        printf ("%d\n", A);

        #undef B
        #define B 42

        printf ("%d\n", A);

You will see that two different numbers are printed, even though
both printf calls use the same macro A.

Then, how the preprocessor preprocess the macro?
Does it work as follows :
scan the code work by word to find match of any macro, if find,
substitute the word to the macro, then
for that particular word, try all other macros to see if there is any
matches, then continue to scan the rest of the code?

Thanks.
 
E

Eric Sosman

[...]
Then, how the preprocessor preprocess the macro?
Does it work as follows :
scan the code work by word to find match of any macro, if find,
substitute the word to the macro, then
for that particular word, try all other macros to see if there is any
matches, then continue to scan the rest of the code?

That's pretty close. Keep in mind that a macro may expand to
more than one "word." Also, some macros ("function-like macros")
take parameters:

#define SHOW(x) printf("%d\n", (x))
#define VALUE 42
SHOW(VALUE);

In the final line, the compiler sees that SHOW is a macro with one
parameter, and uses VALUE as that parameter. It then expands the
macro, using VALUE wherever x appears in the macro definition:

printf("%d\n", (VALUE));

Rescanning, the compiler sees that VALUE is an "object-like" macro
with no parameters, which it also expands:

printf("%d\n", (42));

The language Standard covers several special cases that I have
not mentioned, but this is the basic idea.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top