what version of cc are you using, with what switches?
what do you get with
cc -E
Main Topics
Browse All TopicsCan someone explain why the code below returns the error:
'SAM1': undeclared identifier
On the 'x=' line?
I would think that the first #define would be fine, then the second #define would be expanded to
#define SAM2 SAM2
which essentially has no effect, so I would expect the error to be
'SAM2': undeclared identifier
after the " int x = SAM1;" is expanded to " int x = SAM2;
but apparently I misunderstand. What does happen in this case?
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Not sure I understand the answer, my "model" involves one pass, but assumes that earlier #defines can affect subsequent #defines, and would work like this:
#define SAM1 SAM2 ... store macro "SAM1->SAM2"
#define SAM2 SAM1 ... expand to "#define SAM2 SAM2" and store macro "SAM2->SAM2"
int x = SAM2; .. expand to "int x = SAM2;" and register error 'undefined identifier SAM2'
instead I got the error 'undefined identifier SAM1', as if the first #define didn't affect the second.
my "model" works with the code below, as follows
#define SAM2 SAM3 ... store macro "SAM1->SAM2"
#define SAM1 SAM2 ... expand to "#define SAM1 SAM3" and store macro "SAM1->SAM3"
int x = SAM1; .. expand to "int x = SAM3;" and register error 'undefined identifier SAM3'
which is the error I got. So it looks like the first #define affected the second #define.
So obviously my "model" is wrong, can you tell me what's wrong with it?
Thanks.
A preprocessor macro (#define) simply causes a textual replacement of the macro identifier with the macro replacement list for any occurrence of the macro identifier that follows the macro, with these exceptions :
(a) if the macro identifier occurs inside a string literal, character constant or comment, it is not replaced (string literals and character constants are seen as single pre-processing tokens at this point)
(b) other macro's are not touched.
Second point to keep in mind, is that after replacing a macro identifier with the macro's replacement list, the replaced text is scanned again for more macro identifiers to be replaced. If more are found, they are also replaced, except if a recursive replacement is found (the same macro identifier is found again), in which case it's not replaced. This avoids infinite recursion loops.
So, for your example :
#define SAM1 SAM2
#define SAM2 SAM1
int x = SAM1;
First, the source is scanned for macro identifiers. One is found (SAM1), and replaced, so we get :
int x = SAM2;
This replaced text (SAM2) is then re-scanned, and we again find a macro identifier (SAM2), that is replaced :
int x = SAM1;
This replaced text (SAM1) is then re-scanned, and we again find a macro identifier (SAM1), which is not replaced, since it is a recursive/nested replacement.
We continue scanning the rest of the source, but find no more macro identifiers, so this is the final result :
int x = SAM1;
which is then passed to the compiler, which doesn't recognize SAM1 as a valid identifier, and throws an error about that.
Similarly for your other example :
#define SAM2 SAM3
#define SAM1 SAM2
int x = SAM1;
First, the source is scanned for macro identifiers. One is found (SAM1), and replaced, so we get :
int x = SAM2;
This replaced text (SAM2) is then re-scanned, and we again find a macro identifier (SAM2), that is replaced :
int x = SAM3;
This replaced text (SAM3) is then re-scanned, but no more macro identifiers are found in it.
We continue scanning the rest of the source, but find no more macro identifiers, so this is the final result :
int x = SAM3;
which is then passed to the compiler, which doesn't recognize SAM3 as a valid identifier, and throws an error about that.
Business Accounts
Answer for Membership
by: mrjoltcolaPosted on 2009-10-28 at 18:50:47ID: 25689977
The ISO preprocessor is not recursive. It simple expands the text once. If there are recursive macros, they are not expanded by the preprocessor, but instead the program is passed to the compiler. If you try GCC with the traditional-cpp flag you get different results than default.
ocs/cpp/Tr aditional- macros.htm l
msmith@dmz ~]$ gcc -traditional-cpp t.c
t.c:10: error: detected recursion whilst expanding macro "SAM1"
t.c:10: error: detected recursion whilst expanding macro "SAM2"
http://gcc.gnu.org/onlined
So what happens in ISO mode is the preprocessor makes one pass and translates to:
Select allOpen in new window