Standard Preprocessing Macros - C++ Preprocessor

C++ examples for Preprocessor:Directive

Introduction

There are several standard predefined preprocessing macros and the most useful are listed in the following Table.

MacroDescription
__LINE__ The line number of the current source line as a decimal integer literal.
__FILE__ The name of the source file as a character string literal.
__DATE__ The date when the source file was processed as a character string literal in the form Mmm dd yyyy. Here,
__TIME__ The time at which the source file was compiled, as a character string literal in the form hh:mm:ss,

You can modify the __LINE__ current line number using the #line directive and subsequent line numbers will increment from that.

For example, to start line numbering at 1000 you would add this directive:

#line 1000

You can use the #line directive to change the string returned by the __FILE__ macro.

Here's an example:

#line 1000 "The program file"

To alter the apparent file name and leave the line numbers unaltered, you could use the __LINE__ macro in the #line directive:

#line __LINE__     "The program file"

You can use the date and time macros to tell when your program was last compiled:

std::cout << "Program last compiled at " <<  __TIME__  << " on "<<  __DATE__  << std::endl;

Related Tutorials