gpt4 book ai didi

c - 宏 : string literal from char literal

转载 作者:太空狗 更新时间:2023-10-29 17:13:18 26 4
gpt4 key购买 nike

在 C 中有没有一种方法可以使用宏从字 rune 字创建字符串文字?

比如我有

'a'

我想创建字符串文字

"a"

澄清问题:

#define A 'a'

write(fd, "x=" CHAR2STRING(A) "\n", 4);

我的问题是如何定义宏CHAR2STRING

最佳答案

–问题评论摘要–

这似乎是不可能实现的。作为替代方案,可以定义字符串文字并编写 STRING2CHAR 宏:

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

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

表达式 *"a" 不是编译时常量(例如,它不能用作具有非自动存储持续时间、非 VLA 数组长度的对象的初始化程序、case 标签或位域宽度),尽管编译器应该能够在编译时对其进行评估(使用 Gcc 和 Clang 进行测试)。


M Oehm 建议和 Matt McNabb .

关于c - 宏 : string literal from char literal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26140446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com