Predefined Identifier __func__ in C
__func__ is one of the predefined identifiers of the C programming language.
According to the C language standard (C99 and C11), the C compiler implicitly adds __func__ in every function to be used in that function to get the function name.
It is Implicitly declared by the translator as if static const char __func__[] = "function-name" appeared immediately before the opening brace of each function definition.
Example 1
#include <stdio.h>
void foo(void){
printf ("%s\n", __func__);
}
void bar (void){
printf ("%s\n", __func__);
foo ();
}
int main (){
bar ();
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
bar
foo

You can also try this code with Online C Compiler
Run Code
In this example, the __func__ method was used to return the names of the functions that were called. The identifier returns the name of the function that was called.
Example 2
#include <stdio.h>
int main (){
printf ("%s\n", __func__);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output
main
Here __func__ identifier is used to get the name of the main function.
Also see, Short int in C Programming
Frequently Asked Questions
Is it necessary to add __func__ predefined identifier?
No, it is not necessary to add Predefined Identifier __func__ in C . the compiler implicitly adds it by default.
Does __func__ methods works on the main function also?
Yes, the __func__ identifier can be used to get the name of the main function also.
In which C language standard is the predefined identifier __func__ implicitly called?
In C language standards C99 and C11, the predefined identifier __func__ implicitly called for each function.
Conclusion
In this article, we have extensively discussed Predefined Identifier __func__ in C and its examples. If you want to learn about the operating systems and file structures in C, you can visit Operating System, Unix File System, File System Routing, and File Input/Output.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc.; you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!