pre-processor string replacement

S

sam_cit

Hi Everyone,

I have the following question regarding string replacement
pre-processor statement,

#define status "23"
#define status "44"

int main()
{
printf("%s\n", status);
}

this works fine and prints 44

where as

#define status "23"
#define status1 status

int main()
{
printf("%s\n", status);
}

prints 23, string replacement occurs on the pre-processor statement
itself, which is not in case 1, why is it so?
Can anyone help me on this?
 
I

Ian Collins

Hi Everyone,

I have the following question regarding string replacement
pre-processor statement,

#define status "23"
#define status "44"

int main()
{
printf("%s\n", status);
}

this works fine and prints 44

where as

#define status "23"
#define status1 status

int main()
{
printf("%s\n", status);
}

prints 23, string replacement occurs on the pre-processor statement
itself, which is not in case 1, why is it so?
Can anyone help me on this?
What did you expect to see?
 
S

sam_cit

What did you expect to see?

i expect first statement to replace status of second statement with
23, so it would be

#define "23" "43"...

which isn't the case, so i though string replacement never happens in
pre-processor statement, however the second example i pointed proves
that understanding is not correct.
 
I

Ian Collins

i expect first statement to replace status of second statement with
23, so it would be

#define "23" "43"...

which isn't the case, so i though string replacement never happens in
pre-processor statement, however the second example i pointed proves
that understanding is not correct.
I see. In the first example you are redefining status (your compiler
did warn you, didn't it?).
 
S

sam_cit

I see. In the first example you are redefining status (your compiler
did warn you, didn't it?).

Yes it did, but my question is had the pre-processor replaced status
with "23" compiler wouldn't have known... why did the pre-processor
choose not to replace status of the second #define with "23"

#define status "23"
#define status "44"
 
R

Richard Heathfield

(e-mail address removed) said:
Hi Everyone,

I have the following question regarding string replacement
pre-processor statement,

#define status "23"
#define status "44"

int main()
{
printf("%s\n", status);
}

this works fine and prints 44

This time, maybe. Next time it might not. Your program exhibits undefined
behaviour. Get the easy stuff right before worrying about the hard stuff.
 
I

Ian Collins

Please trim signatures.

Yes it did, but my question is had the pre-processor replaced status
with "23" compiler wouldn't have known... why did the pre-processor
choose not to replace status of the second #define with "23"
Because you are redefining the token, not using it. As Richard pointed
out, this is undefined.

Get your code to compile without warnings.
 
R

Richard Heathfield

Ian Collins said:
Because you are redefining the token, not using it. As Richard pointed
out, this is undefined.

Actually, what I was referring to was the call to a variadic function
without a valid prototype in scope.
Get your code to compile without warnings.

....after cranking up the warning level to a reasonable height.
 
M

Mark McIntyre

i expect first statement to replace status of second statement with
23, so it would be

#define "23" "43"...

No, the preprocessor doesn't work like that. All you've done is
redefine status.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
A

Alan Curry

Hi Everyone,

I have the following question regarding string replacement
pre-processor statement,

#define status "23"
#define status "44"

This part has been explained already, but the second part, where your
question actually becoms interesting, hasn't:
#define status "23"
#define status1 status

int main()
{
printf("%s\n", status1);
[your correction from another post has been applied to the previous line]
}

prints 23, string replacement occurs on the pre-processor statement
itself, which is not in case 1, why is it so?

It is not so. What really happens macro replacement happens repeatedly.

The definitions that the preprocessor is remembering are conceptually
status => "23"
status1 => status
just like they look in the #defines.

The preprocessor sees the "status1" which is recognizes as a macro, and
replaces it with "status". Then it sees the "status" that is recognizes as a
macro, and replaces it with the quoted 23.
 
T

Thad Smith

i expect first statement to replace status of second statement with
23, so it would be

#define "23" "43"...

There are a few places that macro substitution of preprocessing tokens
doesn't occur. The following are some (there may be others):

1. identifier following #define and #undef
2. argument names in a function-like macro definition
3. identifier following #ifdef and #ifndef
4. identifier following defined operator in #if, #elif statement
5. identifiers following #error
6. identifiers following #pragma STDC or equivalent _Pragma ("STDC ...")
operator (C99 only)
7. inside string literals
8. inside character constants
9. inside h-character-sequence and q-character-sequence (within <> and
"") on #include line
10. identifiers in skipped text after a false #if
(#elif/#ifdef/#ifndef), including text on the same line after #<pp operator>
11. comments (they don't exist in translation phase 4)

Does anyone know of any other places within the program text that macros
are not expanded?
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top