Question

When we do not give the value in a macro substitution.

#define YES
printf("%d",YES)

The value of YES is not given, is it by default 0 or garbage?

Was it helpful?

Solution

You just defined YES to nothing. So the result program after preprocessor should be printf("%d",)

OTHER TIPS

I would be surprised that it compiles

As the code hitting the compiler is

printf("%d",)

!!

The C Preprocessor operates on tokens. The macro definition for YES says to replace the token YES with no token, effectively removing the YES token. So, the result after macro replacement is

 printf ("%d",)

which is a syntax error as you observed.

Unlike uninitialized automatic variables, macro replacements are always well defined, so you cannot have indeterminate values ("garbage") as the result of macro expansion.

Neither, it has no value.

This usage is usually useful in situations like this:

#define _VXWORKS

Later, check if the macro has been defined.

#ifdef _VXWORKS
//do something
#else
//do something else
#endif

nothing

you would use GCC : You can find to like this: gcc prog.c -E -P

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top