By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

pre-processor string replacement

P: n/a
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?

Jan 14 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
>
#define status "23"
#define status1 status

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

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

Jan 14 '07 #2

P: n/a
sa*****@yahoo.co.in wrote:
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?

--
Ian Collins.
Jan 14 '07 #3

P: n/a


#define status "23"
#define status "44"
What did you expect to see?

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

Jan 14 '07 #4

P: n/a
sa*****@yahoo.co.in wrote:
>
>>#define status "23"
#define status "44"

What did you expect to see?

--
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?).

--
Ian Collins.
Jan 14 '07 #5

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

--
Ian Collins.
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"

Jan 14 '07 #6

P: n/a
sa*****@yahoo.co.in 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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 14 '07 #7

P: n/a
sa*****@yahoo.co.in wrote:
>>I see. In the first example you are redefining status (your compiler
did warn you, didn't it?).
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.

--
Ian Collins.
Jan 14 '07 #8

P: n/a
Ian Collins said:
sa*****@yahoo.co.in wrote:
>>>I see. In the first example you are redefining status (your compiler
did warn you, didn't it?).
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.
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 14 '07 #9

P: n/a
On 14 Jan 2007 00:10:18 -0800, in comp.lang.c , sa*****@yahoo.co.in
wrote:
>
>
#define status "23"
#define status "44"
What did you expect to see?

--
Ian Collins.

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
Jan 14 '07 #10

P: n/a
In article <11*********************@q2g2000cwa.googlegroups.c om>,
<sa*****@yahoo.co.inwrote:
>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.

--
Alan Curry
pa****@world.std.com
Jan 14 '07 #11

P: n/a
sa*****@yahoo.co.in wrote:
>
>>#define status "23"
#define status "44"

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"...
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?

--
Thad
Jan 14 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.