Line Control (#line)

A preprocessor line control directive supplies line numbers for compiler messages. It causes the compiler to view the line number of the next source line as the specified number.

A preprocessor #line directive has the form:

>>-#--line--+-decimal_constant--+-----------------+-+----------><
            |                   '-"--file_name--"-' |
            '-characters----------------------------'
 
 

In order for the compiler to produce meaningful references to line numbers in preprocessed source, the preprocessor inserts #line directives where necessary (for example, at the beginning and after the end of included text).

A file name specification enclosed in double quotation marks can follow the line number. 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.

In all C and C++ implementations, the token sequence on a #line directive is subject to macro replacement. After macro replacement, the resulting character sequence must consist of a decimal constant, optionally followed by a file name enclosed in double quotation marks.

Example of the #line Directive

You can use #line control directives to make the compiler provide more meaningful error messages. The following program uses #line control directives to give each function an easily recognizable line number:

/**
 ** This example illustrates #line directives.
 **/
 
#include <stdio.h>
#define LINE200 200
 
int main(void)
{
   func_1();
   func_2();
}
 
#line 100
func_1()
{
   printf("Func_1 - the current line number is %d\n",_ _LINE_ _);
}
 
#line LINE200
func_2()
{
   printf("Func_2 - the current line number is %d\n",_ _LINE_ _);
}

This program produces the following output:

Func_1 - the current line number is 102
Func_2 - the current line number is 202
IBM Copyright 2003