C++ Preprocessor

By | March 15, 2020

Preprocessors are the special piece of code that we use in our program, and a preprocessor processes our program before compilation. There are many processes that include compiling and executing a program in C++.

The process of Compiling a source code and Executing the code:

  • First, we save our source code as .cpp file.
  • And when we compile our source code, before the compiler compiles the code, the preprocessor processes the source code and generates an expanded file.
  • This expanded file includes all the dependencies of the source code, then the compiler compiles this expanded file and generates an object code file.
  • At last, the linker links this object code file to the object code of the library functions to generate the executable file program.exe.

The preprocessors program starts with the hash symbol (#), #include is also a preprocessor directive. Preprocessors are not the general C++ statement that’s why we do not put a semicolon ; after the preprocessor statement.

Apart from #include, #define, and #ifndef are examples of preprocessors.

Types of Preprocessor Directives:

There are 3 major types of Preprocessors directives:

  • Macros
  • File Inclusion
  • Conditional Compilation

Macros

Macros are the preprocessor directive which is used to give a special name to value and the name life remains throughout the program execution.

To specify a macro we use the #define preprocessor directive, and the #define comprise of an identifier name or macro and value, every time the compiler sees the identifier it will replace it with the value.

#define Syntax:

#define identifier_name  value_to_replace_with

Example

#include <iostream>
using namespace std;
#define PI 3.14    //here PI is the macro and 3.14 is the value which replace PI.
int main () 
{
  cout<<"The value of PI is: "<<PI;
  return 0;
}

Output

The value of PI is: 3.14

Behind the Code

In this example using the #define preprocessor directive, we define the macro PI as 3.14 so every time the compiler encounters the PI in the program it substitutes it with 3.14.

Macro with arguments

In the above example, we use #define to define an identifier we can also use #define to define a function.

Example:

#include <iostream>
using namespace std;
#define add(a,b) (a+b)
int main () {
  int sum;
  sum= add(3,4);    // compiler replace add(3,4) with (3+4)
  cout<<"Sum is: "<<sum;
  return 0;

}

Output

Sum is: 7

Behind the Code

In the above example, the compiler substitutes the add(3,4) statement with (3+4).

File Inclusion

File Inclusion Preprocessors are used to include files in the source code, #include is the example of File inclusion, there are two types of file inclusion.

  • Build-in Header Files
  • User Define files

Build-in header files

#include <iostream> here <iostream> is a building header file and using the #include we include the iostream file in our source code that’s why we able to use statements like cout, cin, etc.

Syntax

#include<file_name>

User-defined Files

using the #include directive we can also include other C++ files in our own program.

Syntax

#include"filename"

Conditional Compilation

#endif and #ifdef are the examples of Conditional Compilation Directives, and these directives are used to compile or skip compile a specific part of the program on the basis of condition.

Syntax:

 #ifdef identifier_name
    statement1;
    statement2;
    statement3;
    ……
#endif

Summary

  • preprocessor, process action before the compilation process.
  • The preprocessor starts with the # symbol.

Leave a Reply

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