Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does __func__ return <unknown> in some functions in C?

Tags:

c

gcc

func

c99

I have many different functions, and they all have simple printf statements using __func__ similar to this one:

printf("%s - hello world!", __func__);

Now the problem I am running into is that in some functions it returns <unknown> instead of the function name.

Why is that? Am I doing something wrong? AFAIK __func__ is a part of c99 so I don't understand why it isn't working as advertised.

I am using GCC 4.7.2 in Debian.

like image 345
c00kiemonster Avatar asked Aug 02 '13 11:08

c00kiemonster


People also ask

Why is my function not returning a value in C?

In C there are no subroutines, only functions, but functions are not required to return a value. The correct way to indicate that a function does not return a value is to use the return type "void". ( This is a way of explicitly saying that the function returns nothing. )

What is__ func__ C?

Predefined Identifier __func__ in C Identifier is the name given to an entity in programming to identify it in the program. Generally, identifiers are created by the programmer for efficient working but there are some predefined identifiers that are inbuilt in programming. For example, cout, cin, etc.

How to call a function without parameters in C?

In C, use void no_args(void) to declare a function that truly takes no parameters (and returns nothing).

Which statement must not end with semicolon a #define B variable declaration C assignment D None?

Preprocessor statements don't need a semicolon at the end. If you put one, the preprocessor actually thinks you mean it with a semicolon. If you are confused with #define s for constants, const int would probably be easier to comprehend.


1 Answers

It sounds like a header you are including must be doing something similar to this bug and defining __func__ as follows:

define __func__ "<unknown>"

and so you only see it when you include that header(s). A quick way to test for this would be to use __FUNCTION__ in a section of the code where __func__ does not work. Then you need to narrow it down and figure out which header has the troublesome logic and fix it.

like image 88
Shafik Yaghmour Avatar answered Nov 02 '22 14:11

Shafik Yaghmour