Template Metaprogramming

What Are __FILE__ and __LINE__ in C?

__FILE__ and __LINE__ are predefined macros that give the source file name and line number at compile time. Learn how to log with them in C and C++.

On this page
  1. A first example
  2. Why the file name can look different
  3. A logging macro
  4. func: the function name
  5. In C++
  6. Things to keep in mind

__FILE__ and __LINE__ are predefined macros. __FILE__ expands to a string literal with the name of the source file being compiled. __LINE__ expands to an integer with the current line number. The compiler fills both in, so you do not declare them. They work the same way in C and in C++.

A first example

#include <stdio.h>

int main(void) {
    printf("File: %s\n", __FILE__);
    printf("Line: %d\n", __LINE__);
    return 0;
}

If this is saved as main.c and compiled with gcc main.c, the program prints File: main.c and Line: 5. Line 5 is the line with the second printf. __FILE__ is a string, so it uses %s, and __LINE__ is an integer, so it uses %d.

Why the file name can look different

The text that __FILE__ expands to depends on the name the compiler was given. Running gcc src/main.c gives src/main.c, not main.c. Build scripts and editors often pass full paths, so the output may look different from what you expect.

A logging macro

Logging is the most common use. A macro can wrap both names so that each message says where it came from:

#include <stdio.h>

#define LOG(message) printf("%s:%d: %s\n", __FILE__, __LINE__, message)

int main(void) {
    LOG("starting");
    return 0;
}

The output is main.c:6: starting. A macro expands where it is used, so __LINE__ reports the line of the LOG call, not the line where the macro is defined. The preprocessor itself is described in what is a preprocessor in C.

func: the function name

C99 added __func__, which holds the name of the current function as a string. Unlike __FILE__, it is an identifier that the compiler provides, not a macro.

void check(void) {
    printf("in %s\n", __func__);
}

Calling check() prints in check. Combining __func__ with __FILE__ and __LINE__ gives a log line that names the file, the line and the function.

In C++

C++ supports __FILE__, __LINE__ and __func__ in the same way. Since C++20, the <source_location> header offers std::source_location, which returns the same details as typed values:

#include <iostream>
#include <source_location>

void show_location(const std::source_location loc = std::source_location::current()) {
    std::cout << loc.file_name() << ':' << loc.line() << '\n';
}

int main() {
    show_location();
}

The default argument is evaluated at the call site, so each call reports where it was made. Older compilers may not provide <source_location>, so check your compiler’s C++20 support before relying on it.

Things to keep in mind

  • The values are fixed at compile time. If you move code and recompile, the numbers change.
  • Every file and line you record adds text to the program’s data, so very chatty logging can make a binary larger.
  • A call that spans several lines may report a line number that differs between compilers, so check the output on the compiler you use.

The complete guide to template metaprogramming

Template Metaprogramming in C++
Read the guide

Keep exploring

Guides from other topics

All topics →