Preprocessor question

Curt Wohlgemuth curtw at hpcllca.HP.COM
Fri Jan 20 06:53:03 AEST 1989


ray at ole.UUCP (Ray Berry) writes:
> 
>    Here's an easy question regarding preprocessor string-izing and subsequent
>   rescanning...
> 
> #define VAL 3
> #define STR(x) #x
>    .
>    .
>   puts("val is " STR(VAL));
> - - - - - - - -
>    Turbo C produces "3" for STR(VAL); Microsoft produces "VAL" and 
> leaves it at that.  
>    Which behavior is correct?  Further, if Microsoft's is correct, how can
> one parenthesize a #define'd value to turn it into a character string?

According to ANSI, 'VAL' is substituted into the 'STR' macro, gets quoted,
and then is NOT subject to macro replacement as a result.

If you want to "parenthesize a #define'd value to turn it into a 
character string", try this:


#define VAL 3
#define STR(x) #x
#define XSTR(x) STR(x)

main()
{
   puts("val is " XSTR(VAL));
}


Now after VAL is substituted into XSTR, it has not yet been quoted, so
it is subject to macro replacement.  It appears that the Microsoft
compiler is ANSI-conforming here, whereas the Turbo C one is not.



More information about the Comp.lang.c mailing list