Filename and line information
Changes the source code's line number and, optionally, the current file name, in the preprocessor.
Syntax
1 | #line | lineno | | |
2 | #line | lineno | "filename" | |
Explanation
- Changes the current preprocessor line number to lineno. Expansions of the macro __LINE__ beyond this point will expand to lineno plus the number of actual source code lines encountered since.
- Also changes the current preprocessor file name to filename. Expansions of the macro __FILE__ from this point will produce filename.
Any preprocessing tokens (macro constants or expressions) are permitted as arguments to
If lineno is 0 or greater than 32767 (do C++11) 2147483647 (od C++11), the behavior is undefined.
Notes
This directive is used by some automatic code generation tools which produce C++ source files from a file written in another language.
In that case,
Example
#include <cassert>
#define FNAME "test.cc"
int main()
{
#line 777 FNAME
assert(2+2 == 5);
}
test: test.cc:777: int main(): Assertion `2+2 == 5' failed.
References
- C++23 standard (ISO/IEC 14882:2023):
- 15.7 Line control [cpp.line]
- C++20 standard (ISO/IEC 14882:2020):
- 15.7 Line control [cpp.line]
- C++17 standard (ISO/IEC 14882:2017):
- 19.4 Line control [cpp.line]
- C++14 standard (ISO/IEC 14882:2014):
- 16.4 Line control [cpp.line]
- C++11 standard (ISO/IEC 14882:2011):
- 16.4 Line control [cpp.line]
- C++98 standard (ISO/IEC 14882:1998):
- 16.4 Line control [cpp.line]