finding the name of the function

G

GVK

Hi,
I've written a piece of network code in C. I'm pritting lots of
debugging information, just in case the program is screwed :) Is there
any built-in function that gives the name of the function from which it
is called? For example,

int foo()
{
print_function_name(); /* some built-in system call */
}

should output the name as "foo()" or somewhat similarly. I've been
coding in C for almost 2 and half years but never used such a function
till now. Any help is appreciated.

regards,
GVK
 
C

Christopher Benson-Manica

GVK said:
I've written a piece of network code in C. I'm pritting lots of
debugging information, just in case the program is screwed :) Is there
any built-in function that gives the name of the function from which it
is called? For example,

There is no standard one AFAIK, but __FILE__ and __LINE__ can provide
a decent approximation.
 
B

Ben Pfaff

GVK said:
I've written a piece of network code in C. I'm pritting lots
of debugging information, just in case the program is screwed :) Is
there any built-in function that gives the name of the function from
which it is called? For example,

C99 offers the __func__ predefined identifier. GCC offers the
nonstandard __FUNCTION__.
 
J

Jeremy Yallop

GVK said:
I've written a piece of network code in C. I'm pritting lots of
debugging information, just in case the program is screwed :) Is there
any built-in function that gives the name of the function from which it
is called?

There's no way to do that in C89, but C99 defines __func__, an array
containing the name of the current function:

int foo()
{
puts(__func__);
}

Some compilers provide extensions to do the same thing; for example,
GCC has __FUNCTION__ and __PRETTY_FUNCTION__, which are similar to
__func__.

(I'd like to propose this as an additional FAQ.)

Jeremy.
 
G

GVK

GVK said:
I've written a piece of network code in C. I'm pritting lots of
debugging information, just in case the program is screwed :) Is there
any built-in function that gives the name of the function from which it
is called?

Thanks a lot to all the guys who took time to read my doubt. I got it
working and thanks again to all the gurus who replied. __FUNCTION__ works.

GVK
 
T

those who know me have no need of my name

in comp.lang.c i read:
C99 offers the __func__ predefined identifier. GCC offers the
nonstandard __FUNCTION__.

recent versions of most compilers that had something non-standard also now
provide the newer, standard identifier, including gcc.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top