gpt4 book ai didi

c - 没有 undef 的宏名称转义

转载 作者:太空宇宙 更新时间:2023-11-04 01:59:56 25 4
gpt4 key购买 nike

我想知道是否有任何方法可以转义宏名称并实际显示真实姓名:

#define BUFF_SIZE 500

printf("%d", BUFF_SIZE);

是否可以在不删除定义的情况下不展开 BUFF_SIZE

最佳答案

Stringification :

Sometimes you may want to convert a macro argument into a string constant. Parameters are not replaced inside string constants, but you can use the ‘#’ preprocessing operator instead. When a macro parameter is used with a leading ‘#’, the preprocessor replaces it with the literal text of the actual argument, converted to a string constant. Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification.

#include <stdio.h>

#define BUFF_SIZE 500
#define STR(x) #x

int main(void)
{
printf("%s\n", STR(BUFF_SIZE));
return 0;
}

请注意,您不能使用 %d 打印字符串,请使用 %s

关于c - 没有 undef 的宏名称转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28604368/

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