Back to C Programming
2026-07-125 min read

C - Predefined Identifier __func__

Learn C - Predefined Identifier __func__ step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on the predefined identifier __func__ in C programming! This lesson is designed to help you understand its importance, how it works, and common mistakes to avoid when using it. By the end of this tutorial, you'll be well-prepared to use __func__ in your coding projects and interviews, making debugging easier and more efficient.

Why This Matters

In C programming, identifying functions during runtime can sometimes be challenging due to the lack of built-in support for function names within the code. The predefined identifier __func__ addresses this issue by providing a way to access the name of the current function at runtime. This feature is particularly useful when dealing with complex programs or during debugging sessions, as it allows developers to easily identify and trace the execution flow of their functions.

Prerequisites

Before diving into __func__, you should have a solid understanding of the following C programming concepts:

  • Variables and data types
  • Functions and function prototypes
  • Basic I/O operations (printf())
  • Compilation process in C
  • Control structures such as loops, conditionals, and arrays

Core Concept

Understanding __func__

The predefined identifier __func__ is a part of the C99 standard and represents the name of the current function. It expands to an implementation-defined character string literal that contains the name of the calling function. This means that you can use it to print the name of the current function within that function itself.

Example Usage

Let's take a look at a simple example demonstrating how __func__ works:

#include <stdio.h>

void helloWorld() {
printf("Hello, World! from %s\n", __func__);
}

int main() {
helloWorld();
return 0;
}

When you compile and run this program, it will output:

Hello, World! from helloWorld

As you can see, the __func__ macro expands to the name of the function (helloWorld) where it is called.

Function Pointers and __func__

It's worth noting that when using function pointers, the behavior of __func__ may not be as expected. In such cases, you can consider defining a string literal with the function name to ensure portability.

Limitations

Note that that the behavior of __func__ may vary between different C compilers and platforms. Therefore, it's always a good practice to test your code on multiple systems to ensure portability.

Worked Example

Now let's dive deeper into a more complex example where we use __func__ for debugging purposes:

#include <stdio.h>

void addNumbers(int a, int b) {
int sum = a + b;
printf("%s: Computed sum = %d\n", __func__, sum);
}

void multiplyNumbers(int a, int b) {
int product = a * b;
printf("%s: Computed product = %d\n", __func__, product);
}

int main() {
addNumbers(3, 5);
multiplyNumbers(4, 6);
return 0;
}

When you run this program, it will output:

addNumbers: Computed sum = 8
multiplyNumbers: Computed product = 24

This example demonstrates how __func__ can be used to print the function name and the computed result within each function. This information is useful for debugging purposes, as it helps developers understand the flow of their code and identify potential issues more easily.

Error Handling with __func__

To further demonstrate the use of __func__ in error handling, let's modify the worked example to include an error check for invalid inputs within the addNumbers() and multiplyNumbers() functions:

#include <stdio.h>

void addNumbers(int a, int b) {
if (a <= 0 || b <= 0) {
fprintf(stderr, "%s: Error! Invalid input.\n", __func__);
return;
}
int sum = a + b;
printf("%s: Computed sum = %d\n", __func__, sum);
}

void multiplyNumbers(int a, int b) {
if (a <= 0 || b <= 0) {
fprintf(stderr, "%s: Error! Invalid input.\n", __func__);
return;
}
int product = a * b;
printf("%s: Computed product = %d\n", __func__, product);
}

int main() {
addNumbers(-3, 5);
multiplyNumbers(0, 6);
addNumbers(3, 5);
multiplyNumbers(4, 6);
return 0;
}

When you run this modified program with invalid inputs, it will output error messages like:

addNumbers: Error! Invalid input.
multiplyNumbers: Error! Invalid input.
addNumbers: Computed sum = 8
multiplyNumbers: Computed product = 24

This example demonstrates how __func__ can be used to print the function name and error messages when encountering invalid inputs, making it easier to identify and debug issues in your code.

Common Mistakes

  1. ### Forgetting to include stdio.h

Remember that __func__ is defined in the standard library header file stdio.h, so you must include it at the beginning of your source files.

  1. ### Assuming portability across all compilers

As mentioned earlier, the behavior of __func__ may vary between different C compilers and platforms. Always test your code on multiple systems to ensure portability.

  1. ### Incorrect usage in preprocessor directives

The __func__ macro should not be used within preprocessor directives such as #define, #if, or #ifdef. If you need to access the function name from a preprocessor directive, consider using a workaround like defining a string literal with the function name.

  1. ### Using __func__ inside macros

When using function pointers or macros, it's recommended to define a string literal with the function name instead of relying on __func__. This ensures portability and avoids potential issues.

Practice Questions

  1. Write a program that uses __func__ to print the names of all functions in the main function.
  2. Modify the worked example to include an error check for invalid inputs within the addNumbers() and multiplyNumbers() functions, and use __func__ to print the error message along with the function name.
  3. Write a program that uses __func__ to calculate the factorial of a number up to 10! and prints the result for each recursive call.
  4. Investigate the behavior of __func__ when using function pointers, and provide a workaround for ensuring portability in such cases.
  5. Write a program that uses __func__ to implement a simple command-line calculator with support for addition, subtraction, multiplication, and division operations.

FAQ

### Can I use __func__ in C++?

No, __func__ is specific to the C programming language and is not available in C++. Instead, C++ provides the preprocessor macro __FUNCTION__.

### Is it safe to rely on __func__ for function names in production code?

While __func__ can be useful during development and debugging, it's generally not recommended to rely on it for function names in production code due to its potential inconsistencies across different compilers. Instead, consider using descriptive and consistent function names that are easy to understand and maintain.

### Can I use __func__ inside macros?

No, the __func__ macro should not be used inside other macros or within preprocessor directives such as #define, #if, or #ifdef. If you need to access the function name from a macro, consider using a workaround like defining a string literal with the function name.

### How can I ensure portability when using function pointers and __func__?

When using function pointers, it's recommended to define a string literal with the function name instead of relying on __func__. This ensures portability across different compilers and platforms.