Q:

C program to print the name of the source code file

belongs to collection: C Preprocessors Programs

0

C program to print the name of the source code file

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to print the line number in source code ... >>
<< C program to print the function names defined in t...