J
James Kuyper
Tony said:James Kuyper said:arnuld wrote:
...In C99 you can use the pre-defined identifier __func__. When used, itAlso some time ago some one told me that when I get an error then I can
create a generic function which, when called within some function will
print the name of that function automatically or something like that I
forgot. It had lots of #define(s) . Can someone write that for me ?
behaves exactly as if the following declaration had been inserted
immediately after the '{' at the start of the enclosing function:
static const char __func__[] = "function-name";
This feature was not available in C89, and I know of no way to do what
you're asking for without making use of __func__.
Unfortunately, one does not have any control of how that function name
string will look. ...
True: the standard mandates that it contain the exact name of the
function. Do you want it to contain something else?
... (I have forgotten how it DOES look since I have a macro
that inserts the declaration shown above with the format I want). __FILE__
is even worse though: Maybe I want a full path with the filename and maybe I
don't. So, I don't use the __FILE__ macro either. I do use the __LINE__
macro though (not much to go wrong with that!). I wonder if it is
implementation defined what __FILE__ and __func__ include (?).
The expansion of the __FILE__ macro is not explicitly
implementation-defined, the standard just says that it is the "presumed
name of the current source file". However, the details of file and path
naming conventions are platform-specific, and are outside the scope of
the C standard. Therefore, the C standard neither mandates nor prohibits
including of the full path as part of the file name.
For __func__ the situation is quite difference. A function's name is a
well-defined concept within the context of a C program, and __func__ is
supposed to contain that function name. I don't see any wiggle room for
variant interpretations.
... Finally, as a
minor peeve: why are the specifiers not both consistent in case (__FILE__ &
__FUNC__ or __file__ & __func__)?
The basic reason is that __FUNC__ is a macro, while __func__ is the name
of an object. There's a widespread convention that names in which all
letters are upper case should be used for all macros, and only for
macros (many people treat enumeration constants the same as macros for
this purpose).
The C standard library contains some violations of this convention, but
that's mainly because C has developed over many decades, and that
convention did not become conventional until long after many features of
the C standard library had become fixed in a form that conflicts with
that convention.