C语言 C语言中#line是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2216540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:24:22  来源:igfitidea点击:

What's the meaning of #line in C language?

cc-preprocessorpreprocessor-directive

提问by Yongwei Xing

What's the meaning of #linein the Clanguage? Where would it be used?

#lineC语言中是什么意思?它会被用在什么地方?

回答by falstro

It tells the compiler where the following line actually came from. It's usually only the C preprocessor that adds these, for example, when including a file, it tells the compiler (which is basically only seeing one stream of data) that we're looking at a different file.

它告诉编译器以下行的实际来源。通常只有 C 预处理器添加这些,例如,当包含一个文件时,它告诉编译器(基本上只看到一个数据流)我们正在查看不同的文件。

This may sound strange, but the preprocessor simply inserts the header files where you specify your includes, and the compiler works on the whole thing (all header files concatenated along with your source code), you can check the result of the preprocessor stage if using gcc with gcc -E myfile.c. In there you'll notice it adds a #linedirective whenever you include files, and also whenever it reduces the amount of text fed to the compiler (such as large amounts of comments may be reduced to a single #linedirective, skipping ahead)

这听起来可能很奇怪,但预处理器只是在您指定包含的位置插入头文件,编译器处理整个事情(所有头文件与源代码连接在一起),如果使用,您可以检查预处理器阶段的结果gcc 与gcc -E myfile.c. 在那里,您会注意到它会#line在您包含文件时添加一个指令,并且每当它减少提供给编译器的文本量时(例如,大量注释可能会减少为单个#line指令,跳过)

It is also used by other programs, such as bison/yacc to tell you that the problem (if there's a compile problem) is related to your rules-file at a specific line, which the compiler would otherwise be unable to do, as the bison/yacc generates c-files.

它也被其他程序使用,例如 bison/yacc 来告诉您问题(如果存在编译问题)与特定行的规则文件有关,否则编译器将无法这样做,因为bison/yacc 生成 c 文件。

回答by codaddict

It is called the preprocessor line control directive.

它被称为预处理器行控制指令

The expansions of both __FILE__and __LINE__are altered if a #linedirective is used. It causes the compiler to view the line number of the next source line as the specified number.

两者的扩张__FILE____LINE__是否被改变#line,使用指令。它使编译器将下一个源代码行的行号视为指定的编号。

Its main use is to make the compiler provide more meaningful error messages.

它的主要用途是让编译器提供更有意义的错误信息。

You can find more explanation and a usage example in IBM's documentation.

您可以在IBM 的文档 中找到更多解释和使用示例。

回答by Amirshk

It is a pragmakeyword:

这是一个pragma关键字:

"#linelets you modify the compiler's line number and (optionally) the file name output for errors and warnings. This example shows how to report two warnings associated with line numbers. The #line 200 directive forces the line number to be 200 (although the default is #7) and until the next #line directive, the filename will be reported as "Special". The #line default directive returns the line numbering to its default numbering, which counts the lines that were renumbered by the previous directive."

" #line允许您修改编译器的行号和(可选)错误和警告的文件名输出。此示例显示如何报告与行号相关的两个警告。#line 200 指令强制行号为 200(尽管默认值为 #7),直到下一个 #line 指令,文件名将被报告为“特殊”。#line default 指令将行编号返回到其默认编号,它计算由前一个指令重新编号的行。 ”

回答by sbi

It is mostly used to supply the file names and line numbers of a source file from which a C file (be it header or implementation) was created. Given that, the compiler would emit diagnostics that hint at the source file rather than at the generated file.

它主要用于提供创建 C 文件(无论是头文件还是实现)的源文件的文件名和行号。鉴于此,编译器将发出提示源文件而不是生成文件的诊断信息。

Preprocessors also use this to hint at included headers in a preprocessed file that has these expanded.

预处理器还使用它来暗示包含这些扩展的预处理文件中的标头。

回答by Colin Valliant

It allows you to change the apparent line number of the file.

它允许您更改文件的明显行号。

The only use I can think of for it is to make the line numbers sane after a long series of multi-line macros.

我能想到的唯一用途是在一系列多行宏之后使行号保持理智。

usage is:

用法是:

#line 42

回答by D.P.Sahoo

# is the string injing symbol to the processor c and c++

# 是处理器 c 和 c++ 的字符串注入符号