Preprocessor Directives & Macros in C++

In this tutorials we will study and understand the concept of Preprocessor Directives & Macros in C++ and also see its applications.

Preprocessor Directives & Macros in C++

Preprocessor directives are lines included in the code of programs preceded by a hash sign (#). These lines are not program statements but directives for the preprocessor. The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements. These preprocessor directives extend only across a single line of code. As soon as a newline character is found, the preprocessor directive is ends. No semicolon (;) is expected at the end of a preprocessor directive. The only way a preprocessor directive can extend through more than one line is by preceding the newline character at the end of the line by a backslash (\).

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used.

  • Object-like macros resemble data objects
  • Function-like macros resemble function calls.
Object-like Macros

An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants.
You create macros with the ‘#define’ directive. ‘#define’ is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro’s body, expansion or replacement list. For example,

Function-like Macros

You can also define macros whose use looks like a function call. These are called function-like macros. To define a function-like macro, you use the same ‘#define’ directive, but you put a pair of parentheses immediately after the macro name. For example,

Conditional Compilation

There are several directives, which can use to compile selectively portions of your program’s source code. This process is called conditional compilation. The conditional preprocessor construct is much like the if selection structure. Consider the following preprocessor code:

You can compile a program for debugging purpose and can debugging turn on or off using a single macro as follows:

causes the cerr statement to be compiled in the program if the symbolic constant DEBUG has been defined before directive #ifdef DEBUG. You can use #if 0 statment to comment out a portion of the program as follows:

Conditional Compilation Program Example

Run Online

Trace: Inside main function
The minimum is 30
Trace: Coming out of main function
Predefined C++ Macros

C++ provides a number of predefined macros mentioned below:

__LINE__This contain the current line number of the program when it is being compiled.
__FILE__This contain the current file name of the program when it is being compiled.
__DATE__This contains a string of the form month/day/year that is the date of the translation of the source file into object code.
__TIME__This contains a string of the form hour:minute:second that is the time at which the program was compiled.
Program example of predefined Macros in C++

Run Online

Value of __LINE__ : 5
Value of __FILE__ : main.cpp
Value of __DATE__ : Oct 21 2016
Value of __TIME__ : 01:01:48

Leave a Reply

Your email address will not be published. Required fields are marked *