Embedding macro values in strings

Frank Wales frank at grep.co.uk
Fri Jan 11 07:08:06 AEST 1991


Is it possible to write a macro such that the value of one of its
parameters, which is derived from another macro, is embedded in a string
after pre-processing (and if so, how)?

For example, suppose I have  foo(__LINE__); in my source, and I want that
to expand to  bar("Line n"); where n is substituted with the actual value
of __LINE__ at that point.  Trying

  #define foo(n)	bar("Line "#n)

and invoking as  foo(10); yields  bar("Line 10"); , whereas  foo(__LINE__);
yields  bar("Line __LINE__"); .

The zero-parameter alternative version

  #define foo()         bar("Line "#__LINE__)

is illegal.

Some time playing and reading section A12 of K&RII has been unfruitful.
I get the impression I'm being defeated by the sequencing of pre-processor
phases, but I'd like to be sure that what I'm attempting is impossible
before resorting to other means to achieve what I want.

[All this is under MSC 5 under SCO Unix, a supposed ANSI compiler. I'd much
 prefer an ANSI C solution to a Classic C one.  Also, for this problem,
 gcc's pre-processor would be an awkward choice.  Thanks in advance, etc..]
--
Frank Wales, Grep Limited,             [frank at grep.co.uk<->uunet!grep!frank]
Kirkfields Business Centre, Kirk Lane, LEEDS, UK, LS19 7LX. (+44) 532 500303



More information about the Comp.lang.c mailing list