Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Predefined Identifier __func__ in C
The __func__ is a predefined identifier in C that provides the name of the current function. It was introduced in C99 standard and is automatically available in every function without any declaration.
Syntax
__func__
The __func__ identifier is implicitly declared as if the following declaration appears at the beginning of each function −
static const char __func__[] = "function-name";
Example 1: Basic Usage
Here's a simple example showing how __func__ returns the current function name −
#include <stdio.h>
void function1(void) {
printf("Current function: %s<br>", __func__);
}
void function2(void) {
printf("Current function: %s<br>", __func__);
function1();
}
int main() {
printf("Current function: %s<br>", __func__);
function2();
return 0;
}
Current function: main Current function: function2 Current function: function1
Example 2: Debugging with __func__
The __func__ identifier is particularly useful for debugging and logging purposes −
#include <stdio.h>
void calculateSum(int a, int b) {
printf("Entering function: %s<br>", __func__);
int sum = a + b;
printf("Sum calculated in %s: %d<br>", __func__, sum);
}
int main() {
printf("Starting program in: %s<br>", __func__);
calculateSum(10, 20);
printf("Program ending in: %s<br>", __func__);
return 0;
}
Starting program in: main Entering function: calculateSum Sum calculated in calculateSum: 30 Program ending in: main
Example 3: Combined with Other Predefined Identifiers
C provides other similar predefined identifiers that work well with __func__ −
#include <stdio.h>
void displayInfo() {
printf("Function: %s<br>", __func__);
printf("File: %s<br>", __FILE__);
printf("Line: %d<br>", __LINE__);
}
int main() {
displayInfo();
return 0;
}
Function: displayInfo File: main.c Line: 6
Key Points
-
__func__is automatically available in every function without declaration - It returns the name of the lexically enclosing function as a string
- Cannot be redefined or assigned to another value
- Available since C99 standard
- Useful for debugging, logging, and error reporting
Conclusion
The __func__ predefined identifier is a valuable tool for debugging and function identification in C programs. It automatically provides the current function name as a string constant, making it ideal for logging and error tracking purposes.
