C++ Preprocessor Directives

C++ Preprocessor Directives: A C++ preprocessor directives are the lines that are included in a program which begins with the # and it is different from the typical source code. They are invoked by the compiler to process the programs before compilation. And the preprocessor directive is placed at the top of the source code in a separate line beginning with the character # and followed by a directive name.



C++ Preprocessor Directives

While the preprocessor directive statement cannot be with a semicolon. Some of the examples of preprocessor directives are #define, #include, #indef, etc.

Types of Preprocessor Directives

There are different types of preprocessor directives present in C++. They are as follows:

  • Macros
  • File Inclusion
  • Conditional Compilation
  • Other directives

1) Macros

Macros are a piece of code in a program that is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘#define’ directive is used to define a macro.

Syntax: #define macro-name replacement-text 

Example:

#include<iostream> 
//macro definition 
#define LIMIT 5 
int main() 
{ 
    for(int i = 0; i < LIMIT; i++) { 
        std::cout << i << "\n"; 
    } 
  
    return 0; 
}

Output:

0
1
2
3
4

Macros with arguments

If you want we can also pass arguments to macros. Macros defined with arguments works similarly as functions.

Example:

#include<iostream> 
//macro with parameter 
#define AREA(l, b) (l * b) 
int main() 
{ 
    int l1 = 10, l2 = 5, area; 
    area = AREA(l1, l2); 
    std::cout << "Area of rectangle is: " << area; 
    return 0; 
}

Output:

Area of rectangle is: 50

2) File Inclusion

The file inclusion preprocessor tells the compiler to include a file in the source code program. And there are other two files which are useful for including.

1. Header file (or) standard files

2. User-defined files

1. Header File (or) Standard Files

The files contains definition of pre-defined functions like printf(), scanf() etc. These files must be included for working with these functions. A different function is declared in different header files.

Syntax: #include< file_name >

2. User-Defined Files

If you feel like that program code is very large. Then you can convert into smaller files and include whenever needed. These types of files are known as User-defined files.

Syntax: #include”filename”

And we are also having another two directives which are not used mostly. They are as follows:

3) Conditional Compilation

These help to compile the specific block of a program (or) skip the compilation based on some conditions. So this process can be done by using two commands such as ‘ifdef’ and ‘endif’.

Syntax:

#ifdef macro_name
statement1;
statement2;
statement3;
.
.
.
statementN;
#endif

Example:

#include<iostream>
using namespace std;
#define DEBUG
#define MIN(a,b) (((a)<(b)) ? a : b)
int main () {
   int i, j;
   
   i = 100;
   j = 30;

#ifdef DEBUG
   cerr<<"Trace: Inside main function" << endl;
#endif

#if 0
   /*This is commented part */
   cout<< MKSTR(HELLO C++) << endl;
#endif

   cout <<"The minimum is " << MIN(i, j) << endl;

#ifdef DEBUG
   cerr <<"Trace: Coming out of main function" << endl;
#endif

   return 0;
}

Output:

The minimum is 30
Trace: Inside the main function
Trace: Coming out of the main function

4) Other Directives

#undef Directive

It is used for defining the existing macro.

Syntax: #undef LIMIT

After using this statement every “ifdef LIMIT” statement will evaluate to false.

 #pragma Directive

It is used to turn on or off some of the features and it is a compiler-specific which means it varies from compiler to compiler.

Predefined C++ Macros

Macro Description
__LINE__ It contains the current line number of the program when it is being compiled.
__FILE__ The current filename of the program when it is being compiled.
__DATE__ A string of the form month/day/year is the date of the translation of the source file into object code.
__TIME__ A string of the form hour:minute: second that is the time at which the program was compiled.

Example:

#include <iostream>
using namespace std;
int main () {
   cout<<"Value of __LINE__ : "<< __LINE__ << endl;
   cout<<"Value of __FILE__ : "<< __FILE__ << endl;
   cout<<"Value of __DATE__ : "<< __DATE__ << endl;
   cout<<"Value of __TIME__ : "<< __TIME__ << endl;
   return 0;
}

Output:

Value of __LINE__ : 6
Value of __FILE__ : test.cpp
Value of __DATE__ : Feb 22 2018
Value of __TIME__ : 18:52:48