Preprocessor question

Larry Jones scjones at sdrc.UUCP
Fri Jan 20 08:41:44 AEST 1989


In article <531 at ole.UUCP>, ray at ole.UUCP (Ray Berry) writes:
> 
> #define VAL 3
> #define STR(x) #x
> - - - - - - - -
>    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?

Microsoft is correct (Horray!!!); the stringize operator
stringizes the actual argument.  To stringize the replacement you
just need one more level of indirection:

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

Now when you write REPSTR(VAL) it gets replaced with STR(3) which
is rescanned and replaced with "3".

----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones at sdrc.UU.NET
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150                  AT&T: (513) 576-2070
"When all else fails, read the directions."



More information about the Comp.lang.c mailing list