2

I found __FUNCTION__ macro that will give the name of currently executing function, But is there any macro available to print the name of parent function (a function that invoked currently running function) ?

void print_error_message(const char *msg)
{
    printf("%s : %s\n",__FUNCTION__,msg);
}

int main()
{
    print_error_message("failed to connect");
    return 0;
}

The above program will give output :

print_error_message : failed to connect

But what I actually required is

main : failed to connect

because main() invoked print_error_message()

I don't want to pass one more argument to function print_error_message() like print_error_message(__FUNCTION__, msg), what I am expecting is a similar macro or function to retrieve the name of the caller function.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • you can try with this https://stackoverflow.com/questions/16100090/how-can-we-know-the-caller-functions-name – Jahangir Nov 26 '19 at 11:20
  • 2
    Note that the standard name is `__func__`, which is a pre-defined identifier (see C11 [§6.4.2.2 Predefined identifiers](http://port70.net/~nsz/c/c11/n1570.html#6.4.2.2)). The names `__FUNCTION__` and `__PRETTY_FUNCTION__` are GCC extensions. See [What are the differences between `__PRETTY_FUNCTION__`, `__FUNCTION__` and `__func__`?](https://stackoverflow.com/questions/4384765/whats-the-difference-between-pretty-function-function-func) – Jonathan Leffler Nov 26 '19 at 11:49
  • 1
    Additionally, the GCC documentation on [function names as strings](https://gcc.gnu.org/onlinedocs/gcc/Function-Names.html) notes: _ These identifiers are variables, not preprocessor macros, and may not be used to initialize char arrays or be concatenated with string literals._ – Jonathan Leffler Nov 26 '19 at 11:55
  • Why the `ansi` tag? – alk Nov 26 '19 at 13:00

4 Answers4

7

There's no standard way to get the name from the call-stack, and if debug information is stripped or otherwise unavailable there's possible no way at all to get the name.

As a simple workround, you could use macros:

#define print_error_message(msg) actual_print_error_message(__FUNCTION__, (msg))

void actual_print_error_message(const char *func, const char *msg)
{
    printf("%s : %s\n",func,msg);
}

Note that __FUNCTION__ is not standard C, it's an extension. For standards-compliance use the C99 special predefined variable __func__.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

You could let the caller pass its function name as a parameter. For example:

void print_error_message(const char *msg, const char *caller)
{
    printf("%s : %s\n",caller,msg);
}

int main()
{
    print_error_message("failed to connect", __FUNCTION__);
    return 0;
}
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
1

No, it is not available and it is not possible.

Because value of a macro like __FUNCTION__ is calculated in preprocessing phase and what you want to do is not possible in that phase.

Afshin
  • 8,839
  • 1
  • 18
  • 53
1

I don't want to pass one more argument to function [...] I am expecting is a similar macro or function to retrieve the name of the caller function.

This does not exist, at least not as part of the C standard.

On Linux GCC provides the capability to pull backtrace info. Perhaps this helps you.

alk
  • 69,737
  • 10
  • 105
  • 255