这是代码:
#include <stdio.h>
#define f(x,y) \x##y
int main(int argc, char* argv[])
{
return 0;
}
f(,)
我的 friend 问了我这个问题,但我没有明确的答案。
最佳答案
反斜杠是一个有效的预处理器标记(“每个不能是上述字符之一的非空白字符”),但它不能转换为任何有效标记(它不在 punctuator< 列表中
s)。除了在行尾使用之外,反斜杠在字符和字符串文字之外的 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.
所以程序的格式不正确。如果生成了可执行文件,尝试执行它将会出现未定义的行为。
如果没有反斜杠,程序将是格式良好的。两个空宏参数的标记串联是明确定义的;它被替换为空。
关于c - 这段代码里有UB吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41863528/