Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ cool macro definitions?

Tags:

c++

c

macros

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.

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

like image 805
anon Avatar asked Feb 06 '10 09:02

anon


2 Answers

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 :-).

like image 111
Alok Singhal Avatar answered Oct 12 '22 12:10

Alok Singhal


Common GCC macros.

like image 30
sud03r Avatar answered Oct 12 '22 14:10

sud03r