By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

determining function name in runtime

P: n/a
Hi all

is there any way the determine which function currently is executing in
a process ?

For example we can find the file name using __FILE__
and line number using __LINE__ macros

simillarly is there any macro which gives the function name

thanks shyam

Mar 2 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a

shyam wrote:
Hi all

is there any way the determine which function currently is executing in
a process ?
I assume you mean program...

For example we can find the file name using __FILE__
and line number using __LINE__ macros

simillarly is there any macro which gives the function name


Yes, and it's __func__ (note double underscores before and after).

--
BR, Vladimir

Mar 2 '06 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote:
shyam wrote:
Hi all

is there any way the determine which function currently is executing in
a process ?


I assume you mean program...
For example we can find the file name using __FILE__
and line number using __LINE__ macros

simillarly is there any macro which gives the function name


Yes, and it's __func__ (note double underscores before and after).


Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.
- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEBwFDagVFX4UWr64RAlQdAJ9XT4qQS65GzFFRPO0RDF qxskDA+wCdGWGd
evG1lEWtpMvGxVdvP/Y97RM=
=vy6r
-----END PGP SIGNATURE-----
Mar 2 '06 #3

P: n/a

"Lew Pitcher" <Le*********@tdsecurities.com> wrote in message
news:8j*******************@news20.bellglobal.com.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote: -snip

, and it's __func__ (note double underscores before and after).
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.


Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>

--
MrG{DRGN}
Mar 2 '06 #4

P: n/a

MrG{DRGN} wrote:
"Lew Pitcher" <Le*********@tdsecurities.com> wrote in message
news:8j*******************@news20.bellglobal.com.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote:

-snip

, and it's __func__ (note double underscores before and after).

Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.


Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>


Yes it is.
<OT>I know nothing about M$VC, but it may provide it as an
extension?<OT>

Mar 2 '06 #5

P: n/a
Ico
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?

--
:wq
^X^Cy^K^X^C^C^C^C
Mar 2 '06 #6

P: n/a

Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?


It is easy for the preprocessor to know the current file and line.
However, it can not known the function name (even it can not known what
is a function) if it doesn't knowns C syntax (and the preprocessor
doesn't).

Mar 2 '06 #7

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?


My copy of the draft C99 standard doesn't give a reason.

My /guess/ is that __FILE__ and __LINE__ can be implemented as macros
because they do not require any interpretation of the C code (they are
strictly derived from the mechanics of reading the source files).

OTOH, __func__ requires the compiler's interpretation of the code in
order to work, so it cannot be implemented in the same manner as __FILE_
and __LINE__.
- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEByGZagVFX4UWr64RAjQJAJ9unyR7P+JPi14YwCi+eK aYF+VeMgCgs0N/
yRzUKfTTmLVMySmRWJUv6us=
=KubY
-----END PGP SIGNATURE-----
Mar 2 '06 #8

P: n/a
"tmp123" <tm****@menta.net> writes:
Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?


It is easy for the preprocessor to know the current file and line.
However, it can not known the function name (even it can not known what
is a function) if it doesn't knowns C syntax (and the preprocessor
doesn't).


s/doesn't/might not/

Several implementations have provided such a macro. While they /may/
have been handled differently from other macros (I don't actually
know), they were still handled as string literals, and thus you could
make use of the concatenation feature, etc.

But I think the reason you give above is at least why the standard
didn't want to /require/ __func__ to be a macro (and so they required
it to be something else).

-Micah
Mar 2 '06 #9

P: n/a
On 2006-03-02, MrG{DRGN} <Ia****@here.com> wrote:

"Lew Pitcher" <Le*********@tdsecurities.com> wrote in message
news:8j*******************@news20.bellglobal.com.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vladimir S. Oka wrote:

-snip

, and it's __func__ (note double underscores before and after).

Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Thus,
char *where_I_am = "I am at line " __LINE__ "\n";
would work, but
char *where_I_am = "I am in function " __func__ "\n";
would not.

OTOH,
printf("I am at line %d in function %s\n",__LINE__,__func__);
will work.


Let me guess, __func__ is a C99 feature?
<OT> (which means I can't use it with my compiler msvc 6.0).<OT>


No, but i have a suggestion:

#if __STDC_VERSION__ < 199901l
#define WHERE_FMT __FILE__ ":%d"
#define WHERE_ARGS __LINE__
#else
#define WHERE_FMT __FILE__ ":%s:%d"
#define WHERE_ARGS __LINE__,__func__
#endif

fprintf(stderr,"Line printed to stderr at "WHERE_FMT"\n",WHERE_ARGS);

will print file.c:function:42 on c99 systems, file.c:42 on others.
Mar 2 '06 #10

P: n/a
On 2006-03-02, Lew Pitcher <Le*********@tdsecurities.com> wrote:

Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:
Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.

Is there a reason why __func__ is not a macro ?


My copy of the draft C99 standard doesn't give a reason.


That's because that's not the right place to look
http://www.open-std.org/jtc1/sc22/wg...onaleV5.10.pdf

lines 29-31 on page 50 [PDF page 57] read as follows:

A new feature of C99: C99 introduces predefined identifiers, which have
block scope (as distinct 30 from predefined macros which have file
scope), and one such predefined identifier, __func__, which allows the
function name to be used at execution time.

That seems to be saying that the reason is because a macro has file
scope, and thus couldn't have a different value on different lines
unless undefined and redefined by the user.

Note, however, that string literal concatenation takes place in phase 6,
whereas semantic analysis [which is when { } becomes a block scope, etc]
is phase 7.
Mar 2 '06 #11

P: n/a
On Thu, 02 Mar 2006 21:48:10 +0000, Jordan Abel wrote:
No, but i have a suggestion:

#if __STDC_VERSION__ < 199901l
#define WHERE_FMT __FILE__ ":%d"
#define WHERE_ARGS __LINE__
#else
#define WHERE_FMT __FILE__ ":%s:%d"
Micro correction: swap %s and %d (or __LINE__ and __func__ below)
#define WHERE_ARGS __LINE__,__func__
#endif

fprintf(stderr,"Line printed to stderr at "WHERE_FMT"\n",WHERE_ARGS);


--
Ben.
Mar 3 '06 #12

P: n/a
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-02, Lew Pitcher <Le*********@tdsecurities.com> wrote:

Ico wrote:
Lew Pitcher <Le*********@tdsecurities.com> wrote:

Note: Unlike __FILE__ and __LINE__, __func__ is not a macro, but a
predefined identifier of local (to the function) scope.
Is there a reason why __func__ is not a macro ?


My copy of the draft C99 standard doesn't give a reason.


That's because that's not the right place to look
http://www.open-std.org/jtc1/sc22/wg...onaleV5.10.pdf

lines 29-31 on page 50 [PDF page 57] read as follows:

A new feature of C99: C99 introduces predefined identifiers, which have
block scope (as distinct 30 from predefined macros which have file
scope), and one such predefined identifier, __func__, which allows the
function name to be used at execution time.

That seems to be saying that the reason is because a macro has file
scope, and thus couldn't have a different value on different lines
unless undefined and redefined by the user.


__LINE__ has a different value on different lines, and the
preprocessor has no problem handling that.

__func__ isn't a macro because making it one would have required the
preprocessor (more precisely, translation phase 4) to understand more
of the language grammar than is currently required.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 3 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.