Preprocessor In C++

Understanding c preprocessor includes the source code which is  written by programmers is stored in the file “program.c”. The file that is stored then processed by preprocessors and an expanded source code file is generated as named program.

Advertisements

This expanded file is compiled by the compiler and an object code file is generated as named program .obj. Finally, the linker links this object code file to the object code of the library functions to generate the executable file program.exe. 

The preprocessor is best thought of as a separate program that manipulates the text in each code file. When the preprocessor runs, it scans through the code file (from top to bottom), looking for preprocessor directives.

Figure 1 – Preprocessor in C++

What are C++ preprocessor directives ?

Preprocessor programs provide preprocessors directives which tell the compiler to preprocess the source code before compiling.

  • All these preprocessor directives begin with a ‘#’ (hash) symbol.
  • The ‘#’ symbol indicates that, whatever statement starts with #, is going to the preprocessor program,
  • Preprocessor program will execute this statement.

Examples of some preprocessor directives are: 

  • #include, 
  • #define,
  •  #ifndef  etc.

Remember that # symbol only provides a path that it will go to the preprocessor, and command such as include is processed by preprocessor program. For example, include will include extra code to your program. We can place these preprocessor directives anywhere in our program. 

What are the types of preprocessor directives ?

There are 4 main types of preprocessor directives:

  • Macros
  • File Inclusion
  • Conditional Compilation
  • Other directives

What are macros ?

Macros are a piece of code in a program which 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,

Example #1

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

In the above program, when the compiler executes the word LIMIT it replaces it with 5. The word ‘LIMIT’ in the macro definition is called a macro template and ‘5’ is macro expansion. 

What is file inclusion directives ?

This type of preprocessor directive tells the compiler to include a file in the source code program. There are two types of files which can be included by the user in the program. Header File or Standard files: These files contains definition of pre-defined functions like printf(), scanf() etc. These files must be included for working with these functions. Different function are declared in different header files. For example standard I/O(Input/Output) functions are in ‘iostream’ file whereas functions which perform string operations are in ‘string’ file. 

Syntax:

#include< file_name >

The file_name is the name of file to be included. The ‘<‘ and ‘>’ brackets tells the compiler to look for the file in standard directory.

Example #2

‘Main.cpp’
#include<stdio.h>  
#include <iostream.h>
//We included “Circle.h” file_name
#include "Circle.h"

int main()  
{  
    float r;  
  
    cout << "Enter radius of Circle\n");  
    cin >> r;  
  
    cout << "Area of Circle is\n" << circle_area(r, PI)) endl;  
  
    return 0;  
}  


‘Circle.cpp’
double circle_area(float r, float PI)  
{  
 return( PI * r * r );
}  


‘Circle.h’
#define PI 3.14159265358979323846
double circle_area(float, float);

Output:

Enter radius of Circle
5
Area of Circle is 78.539819

‘Main.c’

This is main C program where we include Circle.h file. By including this file we also include macro definition for PI and also the function to calculate area of circle.

Preprocessor includes all the content of Circle.h into Main.cpp source code before passing the code to the compiler

Advertisements

We have to include Main.cpp, Circle.cpp and Circle.h are all in the same directory/folder.

user defined files:

When a program becomes very large, it is good practice to divide it into smaller files and include whenever needed. These types of files are user defined files. These files can be included as:

#include”filename”.

What is conditional compilation ?

Conditional Compilation directives are type of directives which helps to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ifdef‘ and ‘endif‘. 

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

If the macro with name as ‘macroname‘ is defined then the block of statements will execute normally but if it is not defined, the compiler will simply skip this block of statements while executing.

Example #3

#include <stdio.h>
#include <iostream.h>
void main ()
{
    cout << "Condition";
    #if 5<8!=1
     cout << "Works";
     cout << "needed";
    #endif
    cout << "Fine";
}

Output:

Condition Fine

Other Directives

Apart from the above directives there are two more directives which are not commonly used. These are: 

#undef Directive:

The #undef directive is used to undefine an existing macro. This directive works as:

#undef LIMIT

#pragma startup and #pragma exit:

These directives helps us to specify the functions that are needed to run before program startup( before the control passes to main()) and just before program exit (just before the control returns from main()). #pragma startup and #pragma exit doesn’t work in GCC compiler.

Example #4

#include <bits/stdc++.h>
using namespace std;    
void first1();
void second2();
  
#pragma startup first1
#pragma exit second2
  
void first1()
{
    cout << "Inside first1()\n";
}
  
void second2()
{
    cout << "Inside second2()\n";
}
  
int main()
{
    void first1();
    void second2();
    cout << "Inside main()\n";
  
    return 0;
}

Output:

Inside first1()
Inside main()
Inside second2()

Summary

The preprocessor directive of both C and C++ are identical and does the preliminary operations before source files are compiled. Therefore, we can say that they are much faster that the source code and are useful in most cases.

It is often in the hands of C/C++ programmer to chose the procedures to preprocess, based on their need. However, one can assume that speed and efficiency are the reason why we write programs that use C/C++ preprocessor codes.



Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version