没有 undef 的宏名称转义

Macro name escaping without undef

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

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