Wednesday, February 7, 2024

[SOLVED] How to single-quote an argument in a macro?

Issue

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



Answered By - kennytm
Answer Checked By - Timothy Miller (WPSolving Admin)