C compiler directive to reset the compiler's idea of where it's parsing code from.

After #line number [identifier], the compiler believes that the next line of source code actually comes from line number of file identifier. If identifier is missing, the current file name is kept.

This is very useful for programs that generate C source code, so that compiler error messages will refer to a file created by the user, not to some generated file. yacc (or its GNU-free incarnation, bison) and lex (or flex) use this to make error messages refer to the grammar files.

Another program that writes C code is the C preprocessor (if indeed it's a separate program)! The C compiler reads a source file after macros have been expanded (and these macros potentially span multiple lines), and after #includes have been inserted into the file (and these are definitely replaced by more than one line!). Yet error messages still need to appear at the correct location in the user's source.

The solution is just to insert a #line directive whereever needed, to realign the compiler's idea of what the current source line is.

Note that despite beginning with a # mark, #line is not a preprocessor command -- It has to remain for the compiler to read, or it loses its entire purpose!

Many compilers agree to abbreviate "#line" as just "#" (followed by whitespace).

Log in or register to write something here or to contact authors.