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

30,625

Solution 1

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.

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 #line directive 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 #line directive, skipping ahead)

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.

Solution 2

It is called the preprocessor line control directive.

The expansions of both __FILE__ and __LINE__ are altered if a #line directive is used. It causes the compiler to view the line number of the next source line as the specified number.

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.

Solution 3

It is a pragma keyword:

"#line lets 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."

Solution 4

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
Share:
30,625
Yongwei Xing
Author by

Yongwei Xing

Updated on February 22, 2020

Comments

  • Yongwei Xing
    Yongwei Xing about 4 years

    What's the meaning of #line in the C language? Where would it be used?

  • bk1e
    bk1e about 14 years
    In what way is #line a pragma?
  • Tim Čas
    Tim Čas over 9 years
    @bk1e: Although it's syntactically different, it behaves the same way as a #pragma in that it is [also] a compiler, and not just a preprocessor directive. The preprocessor does not remove it, and the compiler interprets it to change some behavior (in this case, error reporting, __LINE__, and __FILE__).
  • Sisir
    Sisir almost 5 years
    And to see the result of the preprocessor stage on a windows system with VisualStudio compiler us command cl.exe /P myfile.c
  • Jonathan Leffler
    Jonathan Leffler over 3 years
    That's not the only usage — you can specify the file name as a string too.