简体   繁体   English

宏:来自 char 字面量的字符串字面量

[英]macro: string literal from char literal

Is there a way in C to create a string literal from a character literal, using a macro? C 中有没有一种方法可以使用宏从字符文字创建字符串文字?

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我的问题是如何定义宏 CHAR2STRING

–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: 作为替代方案,可以定义字符串文字并STRING2CHAR编写STRING2CHAR宏:

#define A "a"
#define STRING2CHAR(s) (*(s))
write(fd, "x=" A "\n", 4);
putchar(STRING2CHAR(A));

or 要么

#define A a
#define XSTR(s) #s
#define SYM2CHAR(sym) (*XSTR(sym))
#define SYM2STRING(sym) XSTR(sym)

The expression *"a" isn't a compile-time constant (so eg it cannot be used as an initializer for an object with non-automatic storage duration, a non-VLA array length, a case label, or a bit-field width), though compilers should be able to evaluate it at compile-time (tested with Gcc and Clang). 表达式*"a"不是编译时常量(因此,例如它不能用作具有非自动存储持续时间的对象的初始化器,非VLA数组长度, case标签或位域尽管编译器应该能够在编译时对其进行评估(使用Gcc和Clang测试)。


Suggested by M Oehm and Matt McNabb . M OehmMatt McNabb建议。

Although I'm quite late to the party, here's a solution I came up with for ASCII (7 bits) characters:虽然我参加聚会的时间很晚,但这是我为 ASCII(7 位)字符提出的解决方案:

#define CHAR2STR(c) ((char*[]){ \
    "\0", "\1", "\2", "\3", "\4", "\5", "\6", "\a", \
    "\b", "\t", "\n", "\v", "\f", "\r", "\16", "\17", \
    "\20", "\21", "\22", "\23", "\24", "\25", "\26", "\27", \
    "\30", "\31", "\32", "\33", "\34", "\35", "\36", "\37", \
    " ", "!", "\"", "#", "$", "%", "&", "'", \
    "(", ")", "*", "+", ",", "-", ".", "/", \
    "0", "1", "2", "3", "4", "5", "6", "7", \
    "8", "9", ":", ";", "<", "=", ">", "?", \
    "@", "A", "B", "C", "D", "E", "F", "G", \
    "H", "I", "J", "K", "L", "M", "N", "O", \
    "P", "Q", "R", "S", "T", "U", "V", "W", \
    "X", "Y", "Z", "[", "\\", "]", "^", "_", \
    "`", "a", "b", "c", "d", "e", "f", "g", \
    "h", "i", "j", "k", "l", "m", "n", "o", \
    "p", "q", "r", "s", "t", "u", "v", "w", \
    "x", "y", "z", "{", "|", "}", "~", "\177", \
})[c]

This can be extended to the full ASCII table with "\180", "\181" ... (all numbers are octals, so no \178 !), but I don't really think it would be useful, considering it's a MACRO.这可以用"\180", "\181" ...扩展到完整的 ASCII 表(所有数字都是八进制,所以没有\178 !),但我真的不认为它有用,因为它是宏。 The trick works thanks to an anonymous string array ("table") initialized in place, which should be optimized by the compiler automatically (if the character is known).该技巧之所以有效,要归功于已就地初始化的匿名字符串数组(“表”),编译器应自动对其进行优化(如果字符已知)。

Not very elegant, but it works: 不是很优雅,但它有效:

#define STRING_ME(tgt, ch) tgt[0]=ch;tgt[1]='\0'

Assumes tgt has space for 2 chars. 假设tgt有2个字符的空间。 Perhaps you could give an example what you want it to look like? 也许您可以举例说明您想要的样子?

You could do 你可以做到

#define A 'a'
#define X(macro) #macro
#define CHAR2STRING(macro) X(macro)

printf("%s\n", CHAR2STRING(A));

you will get 'a' instead of a, but maybe thats ok for you. 你会得到'a'而不是a,但也许那对你好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM