Is there a way in C to create a string literal from a character literal, using a macro?
for example I have
'a'
and I want to create the string literal
"a"
To clarify the question:
#define A 'a'
write(fd, "x=" CHAR2STRING(A) "\n", 4);
My question is how to define the macro CHAR2STRING
You could do
you will get 'a' instead of a, but maybe thats ok for you.
Not very elegant, but it works:
Assumes tgt has space for 2 chars. Perhaps you could give an example what you want it to look like?
–Summary of the comments to the question–
This seems impossible to achieve. As an alternative, the string literal could be defined and a
STRING2CHAR
macro be written instead:or
The expression
*"a"
isn't a compile-time constant (so e.g. it cannot be used as an initializer for an object with non-automatic storage duration, a non-VLA array length, acase
label, or a bit-field width), though compilers should be able to evaluate it at compile-time (tested with Gcc and Clang).Suggested by M Oehm and Matt McNabb.