LINUX内核源码学习笔记——stringify.h文件

linux6.8.6/include/linux/stringify.h文件源码如下:

#ifndef __LINUX_STRINGIFY_H
#define __LINUX_STRINGIFY_H

/* Indirect stringification.  Doing two levels allows the parameter to be a
 * macro itself.  For example, compile with -DFOO=bar, __stringify(FOO)
 * converts to "bar".
 */

#define __stringify_1(x...)	#x
#define __stringify(x...)	__stringify_1(x)

#define FILE_LINE	__FILE__ ":" __stringify(__LINE__)

#endif	/* !__LINUX_STRINGIFY_H */

#define __stringify_1(x...) #x        的作用是:将变量 x 转换成字符;

#define __stringfy(x...) __stringify_1(x) 的作用是:如果变量 x 是宏,可以对宏解引用;

编写 stringify.c 代码如下:

#include <stdio.h>


#define __stringify_1(x...)	#x
#define __stringify(x...)	__stringify_1(x)
#define FOO foo
#define BAR FOO
int main()
{
    printf("%s\n",  __stringify(BAR));
    printf("%s\n",  __stringify_1(BAR));
    return 0;
}

 上面代码执行效果如下:

 

猜你喜欢

转载自blog.csdn.net/PMSM_Driver/article/details/137883971
今日推荐