我刚刚遇到一个C文件,其中包含预处理程序指令和如下所示的行:
# 9 "filename"
Run Code Online (Sandbox Code Playgroud)
我以前从未见过这样的台词.他们的意思是什么?我猜这些是预处理器指令,但预处理器对它们做了什么?
此外,对于某些行,字符串甚至不代表现有的文件名...
我相信这是使用#line预处理器指令的另一种方式。
例如你可以写:
// you could write #line 7 "filename" or
// # 7 "filename" or
// # 7 or
#line 7
int main(void)
{
printf("%d\n", __LINE__);
Run Code Online (Sandbox Code Playgroud)
他们都会给你(在这种情况10下) stdout。
关于“文件名”部分的注释是可选的且未经验证(这就是为什么它可以是任何内容,甚至是不存在的文件)。我提供的链接中解释了它的用法 -
If you specify a file name, the compiler views the next line as part of the specified file. If you do not specify a file name, the compiler views the next line as part of the current source file.