Before we start discussing about __func__, let us write some code snippet and anticipate the output:
Will it compile error due to not defining variable __func__ ? Well, as you would have guessed so far, it won’t give any compile error and it’d print main!
C language standard (i.e. C99 and C11) defines a predefined identifier as follows in clause 220.127.116.11:
“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.”
It means that C compiler implicitly adds __func__ in every function so that it can be used in that function to get the function name. To understand it better, let us write this code:
And it’ll give output as foobar. A use case of this predefined identifier could be logging the output of a big program where a programmer can use __func__ to get the current function instead of mentioning the complete function name explicitly. Now what happens if we define one more variable of name __func__
Since C standard says compiler implicitly defines __func__ for each function as the function-name, we should not defined __func__ at the first place. You might get error but C standard says “undefined behaviour” if someone explicitly defines __func__ .
Just to finish the discussion on Predefined Identifier __func__, let us mention Predefined Macros as well (such as __FILE__ and __LINE__ etc.) Basically, C standard clause 6.10.8 mentions several predefined macros out of which __FILE__ and __LINE__ are of relevance here.
It’s worthwhile to see the output of the following code snippet:
Instead of explaining the output, we will leave this to you to guess and understand the role of __FILE__ and __LINE__!
Please do Like/Tweet/G+1 if you find the above useful. Also, please do leave us comment for further clarification or info. We would love to help and learn 🙂
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Predefined Macros in C with Examples
- Difference between C and Dart
- First element of every K sets having consecutive elements with exactly K prime factors less than N
- Difference between Sentinel and Counter Controlled Loop in C
- Why do we need reference variables if we have pointers
- What are the differences between C and Embedded C?
- Sorting algorithm visualization : Insertion Sort
- Macros and its types in C/C++
- Check if row-major order path of Matrix is palindrome or not
- Draw a smiley face using Graphics in C language
- NULL undeclared error in C/C++ and how to resolve it
- Draw a Chess Board using Graphics Programming in C
- Which will be faster while(1) or while(2)?
- what happens when you don't free memory after using malloc()
Improved By : avsadityavardhan