Macros in ANSI C

Gary Jackoway gary at hpavla.avo.hp.com
Tue Mar 5 04:54:10 AEST 1991


Mojy Mirashrafi writes:

> Here is my question:

> 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'.

> In ANSI C the "'" prevents evaluation of the enclosed characters.
> The above macro will expand to: 's'. Is there a way to escape the "'"s, 
> in ANSI C?

I don't want to get into an argument about whether it should have worked like
you say it did in the past, so I'll just give you a (mildly ugly) solution.

Since we now have the "stringize" # operator, all you have to do is this:

#define CHARIZE(c) (#c)[0]

That'll do it.

> Thanx.

Your welcome, but don't tell anyone I came up with this solution.
				    -

Gary Jackoway



More information about the Comp.lang.c mailing list