Macro expansion : Confusion

S

saurabh29789

I tried this code :

/* File1.c */

#include<stdio.h>
#define _q "
int main()
{
printf(_q ABC _q );
return 0;
}

After executing the following :

gcc -E File1.c

, I get

int main()
{
printf(" ABC ");
return 0;
}

This shows that the macro is expanded as required, but when compiled
=> gcc File1.c gives following errors:

1. Missing terminating " character
2. Missing terminating " character
3. 'ABC' undeclared

Why am I getting the errors even if the expansion is done as was
required ??
 
N

Nate Eldredge

saurabh29789 said:
I tried this code :

/* File1.c */

#include<stdio.h>
#define _q "
int main()
{
printf(_q ABC _q );
return 0;
}

After executing the following :

gcc -E File1.c

, I get

int main()
{
printf(" ABC ");
return 0;
}

I get a warning also:

nate@vulcan:/tmp$ gcc -E foo.c >foo.i
foo.c:2:12: warning: missing terminating " character

But I do get that output, and the output of gcc -E can be compiled
without error.
This shows that the macro is expanded as required, but when compiled
=> gcc File1.c gives following errors:

1. Missing terminating " character
2. Missing terminating " character
3. 'ABC' undeclared

Why am I getting the errors even if the expansion is done as was
required ??

This is weird behavior by gcc, probably related to the fact that (IIRC)
the preprocessing is not done as a separate pass by default, but
integrated with the rest of the compilation. When you run gcc -E
explicitly, you force this pass to be separated, and it wouldn't
surprise me that the behavior is different.

However, your code is not valid C, so as far as the C standard is
concerned, the compiler is free to reject (or accept) it in any way it
sees fit. The definition of a macro must be a sequence of
"preprocessing tokens," and a " character by itself is not a valid
preprocessing token; it has to occur as part of another token, such as a
string literal. 6.4 (3) of the C99 standard says that other occurences
cause undefined behavior.

You could report this as a bug to the gcc people if you like, since it
is at least counterintuitive behavior, but I don't think it's a terribly
serious bug.

Were you actually hoping to write code like this, or is this just a
curiosity? If the former, you probably want to use the standard
"stringification" feature. See the "Stringification" section in `info
cpp', or 6.10.3.2 of the C99 standard. Example:

#include <stdio.h>
#define please_stringify(s) #s
int main(void) {
puts(please_stringify(ABC));
return 0;
}
 
S

saurabh29789

Were you actually hoping to write code like this, or is this just a
curiosity?  If the former, you probably want to use the standard
"stringification" feature.  See the "Stringification" section in `info
cpp', or 6.10.3.2 of the C99 standard.  Example:

You can take it as a curiosity because the stringification feature is
known to me.

Thanks for the explanation.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top