#line Directive

This page was translated by a robot.

The #linedirective affects the predefined constants __LINE__and __FILE__. Usually these constants indicate which line and which file the compiler is currently on. #lineThese values ​​can be explicitly changed with the directive. However, since manipulated line and file specifications can make debugging considerably more difficult, experimental use of the #linedirective is generally NOT ADVISED.




Line: 4, File: test.cpp

Line: 99, File: wurst.h
#include <stdio.h>

int main(){
  printf("Line: %d, File: %s\n", __LINE__, __FILE__);
  #line 99 "wurst.h"
  printf("Line: %d, File: %s\n", __LINE__, __FILE__);
  return 0;
}

Details

The file name is given as a full path. However, it was shortened to the file name here for the sake of clarity..

The purpose of this directive is to give the programmer hints as to which file to consult. This is especially the case when including obsolete files that point to new files. However, the use of the #linedirective is becoming obsolete due to the good documentation of APIs, as well as the improved intelligence of today's compilers, which are mostly able to find the correct places.

Macros can also be specified for the line number:



123
#define LINENUM 123
#line LINENUM
printf("Line: %d\n", __LINE__);

So to teach the preprocessor to just __FILE__change the constant, simply use the following construct:


Line: 124, File: gaga.h
#line __LINE__ "gaga.h"
printf("Line: %d, File: %s\n", __LINE__, __FILE__);

#lineThe line numbers are incremented from the specified line after the occurrence of the directive:


99
100
#line 99
printf("Line: %d\n", __LINE__);
printf("Line: %d\n", __LINE__);