
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
C - Predefined Identifier __func__
The predefined identifier __func__ in C programming is a special identifier introduced in the C99 standard. It provides the name of the current function as a string literal, making error tracking and debugging more convenient.
Debugging and logging frequently require identifying where an issue occurs in the code. To simplify the process, the C99 standard introduced the predefined identifier __func__.
A Simple Example of Using __func__ in a C Program
Before we start discussing __func__ in detail, let us write a sample program and predict its output. The following C program to shows a glimpse of how the Predefined Identifier __func__ works −
#include <stdio.h> int main() { // %s read the string printf("%s", __func__ ); return 0; }
When you run this code, it will produce the following output −
main
The predefined identifier "__func__" represents the name of the current function as a string. Inside main(), it expands to "main", so the program prints the functionâs name.
What is __func__ ?
__func__ is a predefined identifier which is automatically available inside every function. It expands to a const char[] containing the name of the current function.
static const char __func__[] = "function-name";
Unlike other predefined macros such as __FILE__ or __LINE__, __func__ is not a macro, but rather a predefined identifier.
Here is its syntax. It can be used directly inside any function to gets its name as a string.
__func__
Example: Basic Usages
In this example, we use the __func__ identifier to display the function name −
#include <stdio.h> void greet() { printf("Currently in function: %s\n", __func__); } int main() { printf("Currently in function: %s\n", __func__); greet(); return 0; }
Following is the output of the above code −
Currently in function: main Currently in function: greet
Error Logging with __func__
Error logging is the process of recording information about error, warning, or unusual behavior that occurs while a program is running.
Rather than terminating execution or displaying a general error message, programs log the details of the error in a file, console, or logging system. This information helps developers in determining what and where the error occurred.
Example: Error Logging with __func__
In this example, we use __func__ to log error details on the console −
#include <stdio.h> #define LOG_ERROR(msg) \ printf("Error in function %s: %s\n", __func__, msg) void displayData(int x) { if (x < 0) { LOG_ERROR("Negative value not allowed"); return; } printf("Processing value: %d\n", x); } int main() { displayData(10); displayData(-5); return 0; }
Here is its output −
Processing value: 10 Error in function displayData: Negative value not allowed
Key Features of __func__
Given below are some of the key features of __func__ −
- Automatic Availability − It is built into the language. There is no need to include the header file for it.
- Function Scope − Available only inside the function in which it appears.
- String Literal − Represents the function name as a const char[].
- Debugging Friendly − Often used in logging systems to report the exact function where an error occurs.
Conclusion
The predefined identifier __func__, introduced in the C99 standard, provides the name of the current function as a string literal. It is a basic but effective feature that improves debugging, error logging, and program monitoring by allowing developers to quickly identify the code where an error occurred.