c-preprocessor - 为什么预处理器给了一个空间?

标签 c-preprocessor stringification

我想使用预处理器注释一行:

#define open /##*

#define close */

main()
{
        open commented line close
}

当我这样做时 $gcc -E filename.c我期望
/* commented line */ 

但我得到了
/ * commented line */ 

以便编译器显示错误

为什么它会给出不需要的空间?

最佳答案

来自 GNU C 预处理器文档:

However, two tokens that don't together form a valid token cannot be pasted together. For example, you cannot concatenate x with + in either order. If you try, the preprocessor issues a warning and emits the two tokens. Whether it puts white space between the tokens is undefined. It is common to find unnecessary uses of '##' in complex macros. If you get this warning, it is likely that you can simply remove the '##'.



在这种情况下,“*”和“/”不会形成有效的 C 或 C++ 标记。所以它们被发射出来,它们之间有一个空格。

(另外:即使您设法将“注释”插入到 C 预处理器的输出中,您也可能会遇到 C 编译错误。那里不应该有任何注释。)

关于c-preprocessor - 为什么预处理器给了一个空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2391744/

相关文章:

C++ 宏定义包括点?

c - 在预处理输出中保留包含语句

c - libc6 : comma operator in assert macro definition

c - x 宏打印错误的 offsetof() 信息

C预处理器字符串化怪异

c - 在 ## 串联之前评估预处理器 token

使用可变参数和后期扩展创建字符串化宏参数列表

这里可以使用 stringize 宏吗?

将#defined 常量数字转换为字符串

python - 如何在 Python 中动态定义类型/类?