这段代码中有UB吗?
Is there UB in this code?
代码如下:
#include <stdio.h>
#define f(x,y) \x##y
int main(int argc, char* argv[])
{
return 0;
}
f(,)
我的朋友问了我这个问题,但我没有明确的答案。
反斜杠是有效的预处理器标记 ("each non-white-space character that cannot be one of the above"),但它无法转换为任何有效标记(它不在 punctuator
的列表中)。反斜杠在字符和字符串文字之外的 C 程序中没有任何特殊意义,除了在行尾使用。
因此,宏替换将无法满足 §6.4.1 第 2 段中的要求:
Each preprocessing token that is converted to a token shall have the lexical form of a keyword, an identifier, a constant, a string literal, or a punctuator.
所以程序不是 well-formed。如果生成可执行文件,则尝试执行它会出现未定义行为。
如果没有反斜杠,程序会是well-formed。两个空宏参数的标记串联是 well-defined;它被替换为空。
代码如下:
#include <stdio.h>
#define f(x,y) \x##y
int main(int argc, char* argv[])
{
return 0;
}
f(,)
我的朋友问了我这个问题,但我没有明确的答案。
反斜杠是有效的预处理器标记 ("each non-white-space character that cannot be one of the above"),但它无法转换为任何有效标记(它不在 punctuator
的列表中)。反斜杠在字符和字符串文字之外的 C 程序中没有任何特殊意义,除了在行尾使用。
因此,宏替换将无法满足 §6.4.1 第 2 段中的要求:
Each preprocessing token that is converted to a token shall have the lexical form of a keyword, an identifier, a constant, a string literal, or a punctuator.
所以程序不是 well-formed。如果生成可执行文件,则尝试执行它会出现未定义行为。
如果没有反斜杠,程序会是well-formed。两个空宏参数的标记串联是 well-defined;它被替换为空。