In article <11**********************@o5g2000hsb.googlegroups. com>,
saki <sa***********@gmail.comwrote:
>On Apr 11, 11:53 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
>In article <1176317218.306739.29...@n59g2000hsh.googlegroups. com>,
>raghu <raghujin...@gmail.comwrote:
>#define GOOGLE
int main(void)
{
printf("%d",GOOGLE);
return 0;
}
In the above program ,by default GOOGLE should be assigned to
zero..right?
>No. GOOGLE would only be substituted as 0 in evaluating #if
conditions. Otherwise, it is substituted as nothing at all.
Correction: GOOGLE would be substituted as nothing at all
even in #if statements. The implicit substitution of 0 for
identifiers only holds for #if statements when the identifier
has *not* been #define'd .
>That would make your printf statement into
printf("%d",)
which is a syntax error.
>could u just explain a little more as to how it happens
Once you have #define'd something using an "object-like" definition
(that is, there is no parameter list on the #define), then
whereever that macro name occurs that is not in quotes, the
-exact- sequence of characters you #define'd it as will be put
into the line, just as if they had always been there. If you
didn't put in any replacement sequence then there will be nothing
to insert there, and the name will effectively be deleted, as if
you had not written it there at all.
[The above is not quite technically correct in certain cases
involving recursive preprocessing macros, and is not technically
correct in certain cases involving object macros defined using the
# operator, such as #define StringMe(foo) #foo ]
If you have #define'd something, then even if you didn't
specify any replacement text, then any #ifdef preprocessor statement
will know that the macro has been defined, and any use of
the defined() operator on an #if statement will know that the
macro has been defined. For example,
If something has -not- been #define'd, then in any #if statement,
it will be replaced by 0. That does not hold if you have #define'd
it in any way: if you #define'd it, the definition you gave will
be substituted in.
#define GOOGLE
#if defined(GOOGLE)
/* This part -will- be included */
#endif
#ifdef GOOGLE
/* This is the same as above; GOOGLE has been defined so
this part -will- be included */
#endif
#if defined(ROCKS)
/* but ROCKS has not been defined, so this part will -not- be included */
#endif
#if !ELGOOG
/* ELGOOG has not been defined, so it will be replaced by 0. You
then have !0 which is 1 which is true, so the #if will succeed
and this part -will- be included */
#endif
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)