Name

#line

Synopsis

The compiler identifies errors it encounters during compilation by the source filename and the line number in the file. The #line directive can be used to change the filename and line numbering in the source file itself.

Syntax:

#line new_number ["filename"]

From this location in the file onward, lines are counted starting from new_number. If filename is also specified, it becomes the new filename indicated by the compiler in any error messages.

The new filename must be enclosed in quotation marks, and new_number must be an integer constant.

Example:

#line 500 "my_prg.c"

The #line directive is typically used by program generators in translating other kinds of code into a C program. In this way the C compiler’s error messages can be made to refer to the appropriate line and filename in the original source code.

The current effective line number and filename are accessible through the predefined macros _ _LINE_ _ and _ _FILE_ _.

Examples:

printf( "Current source line number: %d\n", _ _LINE_ _ );
printf ( "Source file: %s\n", _ _FILE_ _ );

Get C Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.