__FILE__ --filename

"__FILE__" is expanded by preprocessor to the filename of the file where the source code is written. This depends on what name you specified the C source file at compile time.

__FILE__

Sample to get the file name

#include <stdio.h>

int main (void) {
  printf("%s\n", __FILE__);
}

Compile and run.

gcc -o a a.c && ./a

Output result.

a.c

Compile and run. Relative path.

gcc -o a ../labo/a.c && ./a

Output result.

../labo/a.c

Sample to display an error message and exit the program

A sample that combines the exit function, __FILE__, and __LINE__ to display an error message and exit the program is exit function.

Associated Information