Preprocessor question
Glenn Fowler[eww]
gsf at ulysses.homer.nj.att.com
Fri Jan 20 12:55:05 AEST 1989
In article <9433 at smoke.BRL.MIL>, gwyn at smoke.BRL.MIL (Doug Gwyn ) writes:
> In article <531 at ole.UUCP> ray at ole.UUCP (Ray Berry) writes:
> >#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?
>
> "Before being substituted, each argument's preprocessing tokens are
> completely macro replaced as if they formed the rest of the
> translation unit; no other preprocessing tokens are available."
>
> So VAL is first expanded to 3 then the #3 is performed to produce
> "3".
unless things changed from the 86 draft, macro arguments subject to the
# and ## operators are not expanded before the # and ## operations, so
the above eventually produces:
puts ( "val is VAL" ) ;
to get VAL expanded add one more macro level:
#define STR(x) #x
#define XSTR(x) STR(x)
puts(STR(VAL) " is " XSTR(VAL));
to eventually get:
puts ( "VAL is 3" ) ;
--
Glenn Fowler (201)-582-2195 AT&T Bell Laboratories, Murray Hill, NJ
uucp: {att,decvax,ucbvax}!ulysses!gsf internet: gsf at ulysses.att.com
More information about the Comp.lang.c
mailing list