function name printing

K

Kumaravel

Can I print the current function name in c? something like __LINE__?

for e.g

can I do

printf( " the current file is %s : function is : %s and line # is
:%d\n", __FILE__, __FUNC__, __LINE__);


thanks
kumaravel.
 
H

Harti Brandt

On Mon, 5 Jul 2004, Kumaravel wrote:

K>Can I print the current function name in c? something like __LINE__?
K>
K>for e.g
K>
K>can I do
K>
K>printf( " the current file is %s : function is : %s and line # is
K>:%d\n", __FILE__, __FUNC__, __LINE__);

__func__ is a a const char * and points to the current function name (for
C99). Gcc supported __FUNCTION__ and __PRETTY_FUNCTION__, but that's not
standard and has been deprecated.

harti
 
J

jacob navia

Harti Brandt said:
On Mon, 5 Jul 2004, Kumaravel wrote:

K>Can I print the current function name in c? something like __LINE__?
K>
K>for e.g
K>
K>can I do
K>
K>printf( " the current file is %s : function is : %s and line # is
K>:%d\n", __FILE__, __FUNC__, __LINE__);

__func__ is a a const char * and points to the current function name (for
C99). Gcc supported __FUNCTION__ and __PRETTY_FUNCTION__, but that's not
standard and has been deprecated.

harti

What was __PRETTY_FUNCTION__ ???

Weird name ... :)
 
D

Dominik Zanettin

Can I print the current function name in c? something like __LINE__?
for e.g

yes, in C99 you can use the predefined identifier __func__

regards
zhan
 
H

Harti Brandt

On Mon, 5 Jul 2004, jacob navia wrote:

jn>
jn>"Harti Brandt" <[email protected]> a écrit dans le message de
jn>jn>> On Mon, 5 Jul 2004, Kumaravel wrote:
jn>>
jn>> K>Can I print the current function name in c? something like __LINE__?
jn>> K>
jn>> K>for e.g
jn>> K>
jn>> K>can I do
jn>> K>
jn>> K>printf( " the current file is %s : function is : %s and line # is
jn>> K>:%d\n", __FILE__, __FUNC__, __LINE__);
jn>>
jn>> __func__ is a a const char * and points to the current function name (for
jn>> C99). Gcc supported __FUNCTION__ and __PRETTY_FUNCTION__, but that's not
jn>> standard and has been deprecated.
jn>>
jn>> harti
jn>
jn>What was __PRETTY_FUNCTION__ ???

That gives you the complete function signature (including name space and
classes in C++).

harti
 
M

Martin Ambuhl

Kumaravel said:
Can I print the current function name in c? something like __LINE__?

for e.g

can I do

printf( " the current file is %s : function is : %s and line # is
:%d\n", __FILE__, __FUNC__, __LINE__);

#include <stdio.h>

#define FTRACE printf( " the current file is %s, " \
"function is %s, and line # is %d\n", \
__FILE__, __func__, __LINE__)

void foo1(void)
{
FTRACE;
}

void foo2(void)
{
FTRACE;
}

int main(void)
{
foo1();
foo2();
return 0;
}

[output]

the current file is a.c, function is foo1, and line # is 9
the current file is a.c, function is foo2, and line # is 14
 
M

Martin Ambuhl

jacob said:
"Harti Brandt" <[email protected]> a écrit dans le message de
What was __PRETTY_FUNCTION__ ???

Weird name ... :)

Doubly off-topic: particular compiler (gcc) and wrong language (not C).
Used to print the function name as it appears in the source
(__FUNCTION__) or "in a language-specific way" (__PRETTY_FUNCTION__).
For C++, for example, __PRETTY_FUNCTION__ yields scoping information.
 
O

Old Wolf

Harti Brandt said:
On Mon, 5 Jul 2004, Kumaravel wrote:

K>Can I print the current function name in c? something like __LINE__?
K>
K>for e.g
K>
K>can I do
K>
K>printf( " the current file is %s : function is : %s and line # is
K>:%d\n", __FILE__, __FUNC__, __LINE__);

__func__ is a a const char * and points to the current function name (for
C99).

Actually it is a const char [] containing the current function name
(so you can do "sizeof" on it, and take its address, etc.)
 

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

Similar Threads


Members online

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top