Predefined Identifier __Func__ In C

Predefined Identifier __func__ In C

In C programming, there’s a handy tool called __func__. It automatically tells you the name of the function you’re in. This makes it easier to find and fix problems in your code. This article explores how __func__ works and why it’s helpful for programmers.

To illustrate the working of predefined identifier __func__

Before we begin discussing __func__, We can write some code snippets and anticipate the output:

// C program to demonstrate working of a
// Predefined Identifier __func__
 
#include <stdio.h>
 
int main()
{
    // %s indicates that the program will read strings
    printf("%s", __func__);
    return 0;
}

Output

main

The C language standard, including versions like C99 and C11, defines a predefined identifier as explained in clause 6.4.2.2.

“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.”

In C99 and later versions (including C11), the __func__ identifier is predefined by the C compiler and automatically added to every function. It provides a way for the programmer to access the name of the current function within that function. This can be useful for debugging and error reporting purposes, allowing you to log or print the function name without explicitly specifying it in your code.

Example 1: To illustrate _func_



// C program to demonstrate __func__
#include <stdio.h>
 
// %s is used to read strings
void foo(void) { printf("%s", __func__); }
void bar(void) { printf("%s", __func__); }
 
int main()
{
    foo();
    bar();
    return 0;
}

Output

foobar

Predefined identifiers like __func__ are very useful for logging and debugging purposes, especially in larger programs. They provide a convenient way to access the name of the current function without having to manually specify it, which can be error-prone and tedious, particularly in extensive codebases.

Example 2: To demonstrate the double


// C program to demonstrate double
// predefined identifier or __func__
#include <stdio.h>
 
int __func__ = 10;
int main()
{
    printf("%s", __func__);
    return 0;
}

In C, it’s essential to adhere to the C standard’s specifications, which explicitly state that predefined identifiers like __func__ are automatically defined by the compiler for each function with their respective function names. Attempting to explicitly define or redefine these predefined identifiers results in undefined behavior, and it’s best to avoid such practices.

Additionally, you rightly pointed out the significance of other predefined macros like __FILE__ and __LINE__. These macros, as mentioned in the C standard clause 6.10.8, provide valuable context information about the source file and line number, which is incredibly useful for debugging, error reporting, and tracking code execution. Following the C standard and using these predefined macros as intended is crucial for writing robust and reliable C programs.

Output of the following code snippet:

// C program to demonstrate __FILE__, func, line
#include <stdio.h>
 
int main()
{
    printf("In file:%s, function:%s() and line:%d",
           __FILE__, __func__, __LINE__);
 
    return 0;
}

FAQ-Predefined Identifier __func__ In C

Q1.What is the __ func __ identifier in C?

Ans. In a C program, __func__ is a special identifier created by the compiler. It’s automatically set to the name of the function where it’s used, helping you identify which function is currently being executed.

Q2. What is a predefined function in C?

Ans. Library functions in C, also known as predefined functions, are functions that are already defined in the C libraries. This means you don’t have to write the function’s code yourself; you can simply call these functions to perform specific tasks. They are ready-made tools you can use to make your C programming tasks easier and more efficient.

Q3. What are predefined macros in C language?

Ans. The __OBJC__ macro is like a switch that helps you determine if the Objective-C compiler is being used. It has a value of 1 when Objective-C is in use, allowing you to conditionally include or exclude code based on whether you’re compiling with Objective-C or not.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment