#line

The #line directive allows you to define arbitrary line numbers for the source lines. Normally, the compiler counts lines starting at line number 1; using the #line directive, you can specify an arbitrary line number at any point. The compiler then uses that line number for subsequent counts.

Example

#include <stdio.h>
main()
{
    printf("A\n");          //A

     #line100               //H
    printf("B\n");          //B
    printf("C FILE %s LINE %d\n", __FILE__, __LINE__ );//C
     #line200               //K

    printf("D\n");          //D
    printf("E\n");          //E
}

Explanation

  1. The statement H indicates the #line directive.
  2. The #line number in statement B is taken as 100 and for statement C, it is taken as 101.
  3. The #line number in statement D is taken as 200 and for statement E, it is taken as 201.
  4. If you introduce any error in statement B then the compiler will display the error at #line number 100.
  5. C has provided two special identifiers: __FILE__ and __LINE__, which indicate the file name of the source file and the current line number, respectively.

Point to Remember

  1. #line is used to indicate line numbers which can be used for debugging.