Is it possible to get the caller's name in C ?

S

Sean Fraley

Wei said:
and how to ?

thanks for any help

If you are asking if it is possible to get the name of the calling function
from inside the function that was called, the answer is no.

Sean
 
M

Mabden

Sean Fraley said:
If you are asking if it is possible to get the name of the calling function
from inside the function that was called, the answer is no.

You could pass it in as a parameter...

Or you might be able to make an array of function pointers and loop through
that to find which one it is, but you'd just know it was function 3, for
instance.
 
S

SM Ryan

# and how to ?

A compiled C program is not required to carry that information
and in some executables that information is deliberately deleted.
 
D

David Resnick

Wei said:
and how to ?

thanks for any help

You can't do it portably without passing it in. You could do it
relatively painlessly as follows. Note that the foo macro must not
be in effect when the foo function itself is compiled, easily
enough to arrange if doing this for real. This assumes
you have __func__ supported by your compiler(s), which means
either c99 or an extension. If not, you need to manually
pass in the name

#include <stdio.h>

void foo(const char *caller)
{
printf("foo was called by '%s'\n", caller);
}

#define foo() foo(__func__)

int main(void)
{
foo();
return 0;
}

If you have a specific platform in mind, you should ask in that
group -- a nice solution to your problem could exist if you
don't need portability. <OT> e.g. look into gcc's
__builtin_return_address function or glibc's backtrace/
backtrace_symbols to see if you can get what you want there</OT>

-David
 
T

tigervamp

Wei Li said:
and how to ?

thanks for any help

How about something like this:

#include <stdio.h>
#define PrintParentFunc() printf("Parent Function: %s\n",__func__)

void SubFunc (void);

int main (void) {
PrintParentFunc();
SubFunc();
return 0;
}

void SubFunc (void) {
PrintParentFunc();
}

Assuming of course you are using a compiler which supports the
__func__ predefined identifier (Officially introduced in C99).

Rob Gamble
 
W

Wei Li

It works fine:) appreciate!

tigervamp said:
"Wei Li" <[email protected]> wrote in message

How about something like this:

#include <stdio.h>
#define PrintParentFunc() printf("Parent Function: %s\n",__func__)

void SubFunc (void);

int main (void) {
PrintParentFunc();
SubFunc();
return 0;
}

void SubFunc (void) {
PrintParentFunc();
}

Assuming of course you are using a compiler which supports the
__func__ predefined identifier (Officially introduced in C99).

Rob Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top