I presume there's some reason why the constants cannot, themselves, be
defined as character constants?
That is, does there exist a pp-builtin CHARIFY such that CHARIFY(x) =
'x' ?
Something like
#define CHAR(x) #x [0]
Can't be used inside switch.
There's no simple way I can think of to define a macro that generates a
character constant whose value depends upon the expansion of another
macro. I did, however, manage to come up with this ugly monstrosity,
which cannot possibly qualify as "simple", which I've inserted into some
test code:
#include <stdlib.h>
#define TRICAT(a, b, c) a ## b ## c
#define CHAR(a, b, c) TRICAT(a, b, c)
#define CASE1 x
int main(int argc, char *argv[])
{
if(argc <= 1)
return EXIT_FAILURE;
switch (*argv[1])
{
case CHAR('
, CASE1, '
): break;
default:
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
The key thing that makes this work is that ' immediately followed by a
newline is not recognized as any other type of pre-processing token, so
it counts as a preprocessing token of it's own, under the classification
"each non-white-space character that cannot be one of the above" (6.4p1).
The two-level expansion allows CASE1 to be expanded to x before being
concatenated with the ' tokens.