Search This Blog

Wednesday, February 9, 2011

Predefined Macros in c++

In ANSI c++ , there are some predefined macro that you can used , for example to write to your logs.

 __DATE__ 
A character string literal containing the date when the source file was compiled. The date will be in the form:
   "Mmm dd yyyy"
 __FILE__ 
Defined as a character string literal containing the name of the source file.
 __LINE__ 
Defined to be an integer representing the current source line number.
 __STDC_
Defined if the C compiler conforms to the ANSI standard. This macro is undefined if the language level is set to anything other than ANSI.
 __TIME__ 
Defined as a character string literal containing the time when the source file was compiled. The time will be in the form:
   "hh:mm:ss"
__FUNCTION_
access the name of a currently executing function programmatically.

more macros could be found here Standard Predefined Macro, more about __FUNCTION__  could be found here Retrieve a Function's Name Programmatically

Code sample:
#include 
#ifdef __STDC__
# define CONFORM "conforms"
#else
# define CONFORM "does not conform"
#endif
int main(void)
{
printf("Line %d of file %s has been executed\n", __LINE__, __FILE__);
printf("This file was compiled at %s on %s\n", __TIME__, __DATE__);
printf("This program %s to ANSI standards\n", CONFORM);
}



Yaniv Tzanany

No comments: