Macros in ANSI C

Doug Gwyn gwyn at smoke.brl.mil
Sun Mar 3 14:13:48 AEST 1991


In article <1172 at intelisc.isc.intel.com> mojy at iSC.intel.com (Mojy Mirashrafi) writes:
>In the old C if you wanted to define a macro to convert its parameter to a 
>char you would write a macro like this:
>#define conv(s) 's'
>and if you used "conv(X)" in your code you would get 'X'.

No, what you mean is that certain C preprocessors (notably Reiser's)
incorrectly implemented the C language specification of K&R (1st Ed.)
and some programmers decided to exploit that bug to perform the sort
of "charizing" (more usually, "stringizing") that you show in the
example.

>In ANSI C the "'" prevents evaluation of the enclosed characters.

Also in "K&R C".

>The above macro will expand to: 's'. Is there a way to escape the "'"s,
>in ANSI C?

Not that I know of, because character constants must be treated as
single preprocessing tokens; an attempt to make the preprocessor
handle isolated ' characters produces undefined behavior (standard
section 3.1 Semantics).

X3J11 perceived a genuine need to support "stringizing", and thus
added the # operator for that.  I have never seen a genuine need
for "charizing"; the usual example given is BSD's
	#define CTRL(x) ('x' & 0x1F)
which should all along have been written
	#define CTRL(x) (x & 0x1F)
and invoked with a character-constant argument rather than a source
text character.



More information about the Comp.lang.c mailing list