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.
__func__, which is a pre-defined identifier (see C11 §6.4.2.2 Predefined identifiers). The names__FUNCTION__and__PRETTY_FUNCTION__are GCC extensions. See What are the differences between__PRETTY_FUNCTION__,__FUNCTION__and__func__?ansitag?