Print the function name using __FUNCTION__ macro

S

somenath

Hi ALL ,

I need to print the name of the function as my debug message. I have
tried the following way
#include<stdio.h>
#include<stdlib.h>

#define __STR(x) _VAL(x)
#define _VAL(x) #x
int main (void)
{
puts( __FUNCTION__ __STR( __FILE__));
return 0;

}
But while compiling the program I am getting the following warning .

simple.c: In function `main':
simple.c:8: warning: concatenation of string literals with
__FUNCTION__ is deprecated

Is there any correct way I can print the function name with file
name ?

Regards,
Somenath
 
R

Richard Bos

somenath said:
I need to print the name of the function as my debug message. I have
tried the following way
#include<stdio.h>
#include<stdlib.h>

#define __STR(x) _VAL(x)
#define _VAL(x) #x

You are not allowed to declare identifiers, not even macros, starting
with __ or _ followed by a capital. (And it is a bad idea, because only
legal in some places, to declare an identifier starting with _ and
something else.) Only the implementation itself is allowed to do that.
int main (void)
{
puts( __FUNCTION__ __STR( __FILE__));
return 0;

}
But while compiling the program I am getting the following warning .

simple.c: In function `main':
simple.c:8: warning: concatenation of string literals with
__FUNCTION__ is deprecated

There's no such thing as __FUNCTION__ in ISO C. There is __func__,
though, which is not a macro and not a string literal but an identifier
which declares a static const char array containing the function name.
Is there any correct way I can print the function name with file
name ?

Since __func__ is not a string literal, you can't concatenate it with
another string literal, but what you can do is

printf("%s %s\n", __func__, __FILE__);

or anything equivalent.

Richard
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top