Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
#line |
Set line number | L | Directive | C89 | C90 | C95 | C99 | C11 | |||
__FILE__ |
Compilation file name | L | M | Predefined | C89 | C90 | C95 | C99 | C11 | ||
__func__ |
Function name | L | Predefined | C99 | C11 | ||||||
__LINE__ |
Compilation line number | L | M | Predefined | C89 | C90 | C95 | C99 | C11 |
During translation, two macros can be used to get
information about the translation into the translated
program. Their expansions change during translation.
__FILE__
is a macro expanding to a string literal identifying the
source file in which it appears. __LINE__
is a macro expanding to an integer expression giving the line
number where it is expanded.
Since C99, __func__
is an object internal to the function in which it appears,
giving the name of that function as a null-terminated string
in an array of char
. It is as
if every function definition begins with a hidden
static
string:
void my_func(void) { static const char __func__[] = "my_func"; printf("Inside %s now\n", __func__); }
Altogether, __FILE__
,
__LINE__
and __func__
can be used to build macros that report on the flow of
control through an executing program:
// In foo.c
#include <stdio.h>
#define TRACE(msg) \
fprintf(stderr, "TRACE: %s %s:%d %s\n", \
__func__, __FILE__, __LINE__, msg)
int main(void)
{
TRACE("here");
return 0;
}
TRACE: main foo.c:10 here
- control-line
# line pp-tokens new-line
The preprocessing directive #line
changes the line number and file name reported in diagnostic
messages, including the expansions of __LINE__
and __FILE__
.
This sets only the line number:
#line digit-sequence new-line
This sets the line number and file name:
#line digit-sequence "s-char-sequenceopt" new-line
Anything else of the following form has its tokens scanned for macro invocations, which are expanded, resulting in one of the two forms above:
#line pp-tokens new-line
#line
is most useful in generated source. For example, an
IDL might
be processed by a tool to yield equivalent C code, and
compile-time errors arising in that code might be the result
of errors in the IDL. The tool that translates IDL into C can
insert #line
directives to make the errors refer to lines in the IDL
instead of C.