Macro substitution in character literals

David Michaels david at lpi.liant.com
Sat Dec 15 06:31:11 AEST 1990


In article <11323 at pt.cs.cmu.edu>
mbj at natasha.mach.cs.cmu.edu (Michael Jones) writes:

> How can I write a macro which produces a character literal containing a macro
> parameter?  I'd like to be able to write something like:
>	#define chr(c) 'c'	/* Yes, this is incorrect */
> which for
>	chr(z)
> produces
>	'z'	

ANSI C does not support any way do this, although many older
Classic C compilers (particularly PCC-based compilers) supported
it (as well as macro parameter substitution within string literals).
It should be avoided if at all possible.

LPI's ANSI C compiler (NEW C) has a Classic C option "-xc" which will cause
macro parameter substitution within character constants to be performed.
Another option "-xwc" will cause warnings to be given wherever a function-
like macro is defined such that macro parameter substitution within character
constants *could* take place.  Both of these options may be given at once.

There are also exactly analogous options "-xs" and "-xws" for macro
parameter substitutions within string literals (although there is of
course an ANSI C way to deal with this, i.e the stringize "#" operator).

				-- David Michaels (david at lpi.liant.com)
				   Language Processors, Inc.
				   959 Concord Street
				   Framingham, MA 01701-4613
				   (508) 626-0006



More information about the Comp.std.c mailing list