
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Predefined Identifier __func__ in C
Identifier is the name given to an entity in programming to identify it in the program.
Generally, identifiers are created by the programmer for efficient working but there are some predefined identifiers that are inbuilt in programming. For example, cout, cin, etc.
Here, we will see one of these predefined identifiers of C programming language which is __func__.
The formal definition of __func__ is −
“The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration
static const char __func__[] = “function-name”;
appeared, where function-name is the name of the lexically-enclosing function.”
C program The __func__ is a compiler-generated identifier that is created to identify the function using function name.
Let’s see a few code examples to make the concept more clear,
Example
#include <stdio.h> void function1 (void){ printf ("%s
", __func__); } void function2 (void){ printf ("%s
", __func__); function1 (); } int main (){ function2 (); return 0; }
Output
function2 function1
Explanation − Here, we have used the __func__ method to return the names of the functions that are called. The identifier returns the name of the function in which it is invoked. Both the print statements called for the __func__ for its own method reference.
This identified works on even the main method. Example,
Example
#include <stdio.h> int main (){ printf ("%s
", __func__); return 0; }
Output
main
But this cannot be overwritten i.e. __func__ is reserved for function names only. Using it to store anything else will return error.
Let’s see
Example
#include <stdio.h> int __func__ = 123; int main (){ printf ("%s
", __func__); return 0; }
Output
error
There are other similar functions too in C programming language which do similar work of identification. Some are
__File__ − return the name of the current file.
__LINE__ − returns the number of current line.
Lets see a code to show the implementation
Example
#include <stdio.h> void function1(){ printf("The function: %s is in line: %d of the file :%s
", __func__,__LINE__,__FILE__); } int main(){ function1(); return 0; }
Output
The function: function1 is in line: 3 of the file :main.c
Explanation − these are some general functions that might be useful as we have gathered information regard filename, line of code and function that is currently invoked using the __func__,__LINE__,__FILE__ identifiers.
- Related Articles
- __func__ identifier in C
- What are predefined attributes in C#?
- What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__ in C/C++?
- What is an identifier in C language?
- What are the predefined functions in C language?
- PHP Predefined Variables
- What is an identifier and its rules in C language?
- Organizationally Unique Identifier
- JavaScript this Identifier
- PHP Predefined Mathematical Constants
- Getting the unique identifier for the current managed thread in C#
- C program to calculate sum of series using predefined function
- What is a predefined package in Java?
- What are the rules about using an underscore in a C++ identifier?
- Order MySQL results without identifier?
