tags:

views:

562

answers:

6

Besides __ LINE__ and __ FILE__

are there other useful pre-defined macros, like

__ FUNCTION_NAME__

?

If not, but you know of other cool/useful defined macros (especially for debugging purposes), I'd love to hear about them.

EDIT: some have asked about platform: I'm using gcc/g++ on MacOSX.

+2  A: 

The complete set of predefined macros for MSVC

personally, think __COUNTER__ is pretty cool.

John Knoeller
+1  A: 

If I'm not mistaken, __func__ is not a macro. It's basically a name that's declared as static const char * const __func__ = "function_name"; at the top of any function in which it is used. And you should be using __func__ instead of anything like __FUNCTION_NAME__ because __func__ is part of the C99 standard.

Omnifarious
You are mistaken: The C99 construct is called `__func__`.
Chris Lutz
`__FUNCTION__` is a macro on MSVC.
John Knoeller
@Chris Lutz, thanks. I think my answer is correct now. :-)
Omnifarious
Also, relating to my answer, if your compiler doesn't support C99, under GCC, if the `__GNUC__` macro is a value greater than 2 (or apparently if you're on MSVC) you can `#define __func__ __FUNCTION__` and get the same result.
Chris Lutz
+4  A: 

I can find the following (descriptions from C99 draft, but they are available in C89 too I think):

  • __DATE__: The date of translation of the preprocessing translation unit: a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10. If the date of translation is not available, an implementation-defined valid date shall be supplied.
  • __TIME__: The time of translation of the preprocessing translation unit: a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function. If the time of translation is not available, an implementation-defined valid time shall be supplied.

For the current function name, C99 defines __func__, but __FUNCTION_NAME__ is not a standard macro. In addition, __func__ is not a macro, it's a reserved identifier (6.4.2.2p1):

The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char __func__[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function.

If you're looking for something that's platform-specific: here's a list of gcc's common predefined macros. I like __COUNTER__, which is a unique, sequential integer starting at 0. I think __INCLUDE_LEVEL__ is cool too, but not sure if I can think of a use for it yet :-).

Alok
`__DATE__` and `__TIME__` are available in C++, too.
sbi
@sbi, thanks for the information.
Alok
`#include` recursion could be terminated by `__INCLUDE_LEVEL__`.
Potatoswatter
@Potatoswatter: Yeah I thought about that, but the compiler is going to detect infinite recursion for me anyway. Still, what you say has uses.
Alok
+1  A: 

The specific one you're looking for is called __func__, but it's not exactly a macro, since it's meaning changes depending on where it's seen. It is useful, however, and does look like a macro.

My favorite macro at the moment is __STDC_VERSION__ because it lets me do this:

#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
# define inline
# define register
# if __GNUC__ >= 2 || _MSC_VER >= 1300
#  define __func__ __FUNCTION__
# else
#  define __func__ "<unknown>"
# endif
#endif

Now you can use the C99 keywords inline, register, and __func__ to declare things without having to worry about whether or not the compiler you're using supports that C99 functionality! In reality, the bit for inline is more complex since some compilers define __inline and other such silliness, but you get the general idea.

Also, a useful list of pre-defined macros for identifying compilers, operating systems, and architectures can be found here.

Chris Lutz
Chris, `__LINE__`'s meaning also changes depending upon where it is seen, so a name's expansion being dependent on where it is seen in the source doesn't necessarily imply it can't be a macro. You're right about `__func__` not being a macro, of course.
Alok
+2  A: 

Common GCC macros.

Neeraj
A: 

You can get all the regular pre-defined macros from gcc like this:

$ gcc -dM -E - < /dev/null

Of course this doesn't inlucde special macros like

__FILE__, __LINE__ and __FUNCTION__.
Paul R