引用宏,用于定义字符串 [英] Quote macro for defining string

查看:45
本文介绍了引用宏,用于定义字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以引用宏的内容"?类似于以下用于打印字符串"AAA"的代码.

Is it possible to quote the "content" of a macro? Something like the following code for printing the string "AAA".

#include <stdio.h>
#include <stdlib.h>

#define _L 5
#define _QUOTE(a) #a
#define _TEXT AAA
#define _STRINGAAA _QUOTE(_TEXT)

const char STRINGAAA[ _L ] = _STRINGAAA;

int main(void)
{
    printf( "%s\n", STRINGAAA );
    return EXIT_SUCCESS;
}

推荐答案

在字符串化之前,您需要使用第二组宏来强制替换宏参数:

You need to use a second set of macros to force macro argument substitution before stringization:

#include <stdio.h>
#include <stdlib.h>

#define _L 5
#define _XQUOTE(a) #a
#define _QUOTE(a) _XQUOTE(a)
#define _TEXT AAA
#define _STRINGAAA _QUOTE(_TEXT)

const char STRINGAAA[ _L ] = _STRINGAAA;

int main(void)
{
    printf( "%s\n", STRINGAAA );
    return EXIT_SUCCESS;
}

这篇关于引用宏,用于定义字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆