Home »
C programs »
C preprocessors programs
C program to print the name of the source code file
Here, we are going to learn how to print the name of the source code file using C program?
By Nidhi Last updated : March 10, 2024
Problem Solution
In this program, we will use __FILE__ macro to print the name of the source code file. As we know that, a project contains multiple files, so we can print the name of the source code file from any source file function in the project.
__FILE__ Macro
__FILE__ is a preprocessor macro in C language that is used to get the full path to the current file. It is useful for debugging to generate the log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.
Program
The source code to print the name of the source code file is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to print the name of
// source code file
#include <stdio.h>
int main()
{
printf("Name of source file: %s\n", __FILE__);
return 0;
}
Output
Name of source file: main.c
Explanation
As we know that, a project may contain multiple source code files. Then if we want to print the name of any source code file from any function. Then we can use __FILE__ macro to print the name of the source code file.
C Preprocessors Programs »