我想使用预处理器注释一行:
#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/