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.

Advertisements