gccc-preprocessorstringification

How to single-quote an argument in a macro?


I would like to create a C pre-processor macro that will single-quote the argument. Just like the common used #X.

I want Q(A) to be expanded to 'A'.

I am using gcc on Linux.

Does any one have an idea?

I know # double-quotes. I am looking for a similar mechanism for single-quoting.


Solution

  • The best you can do is

    #define Q(x) ((#x)[0])
    

    or

    #define SINGLEQUOTED_A 'A'
    #define SINGLEQUOTED_B 'B'
    ...
    #define SINGLEQUOTED_z 'z'
    
    #define Q(x) SINGLEQUOTED_##x
    

    This only works for a-z, A-Z, 0-9 and _ (and $ for some compilers).