C++ Pre-Processors

In this C++ tutorial, we are going to discuss the preprocessors, its types and its implementation. Have you thought what happens between writing a program and its compilation? Let us discuss this section in this post.

What is Pre-Processors in C++

Pre-Processor is the part of source code in our program which processed before compilation.

The Code written by programmers is known as Source Code. program.c stores the source code. This file is pre-processed by pre-processors and an expanded source code file is generated name program. Compiler compiles this expanded file and then program.obj is generated automatically which holds object code. Then, Linker generates the executable file program.exe by linking the object code file to object code of library functions.

before compiling Pre-Processor directives provided by pre-processors are processed. All the pre-processor begins with a ‘#’ symbol.

Some of the examples of pre-processor directives are:-

  • #include
  • #indef
  • #define

Main types of pre-processor directives are:-

Macros

Macros are the alternative name given to a piece of code such that whenever we need to use that code we can use its alternative name. It is a time-saving and less manual effort in writing big programs and also helpful in competitive programming.

Do you know?

There is no semi-colon ( ; ) at the end of macro definition.

Syntax :-                 #define alternative_name actual_piece_of_code

Code Snippet

#define ll long long

  int main()
 {
    ll i,j;

     ll n;
       cin>>n;

      ll a[n+1];
  
      for( i=0;i<n;i++)
           cin>>a[i];
}

As in the above code, we used ll as an alternative name where we need to use long long.

File Inclusion

To include a file in source code program we use File Inclusion pre-processor directives. We can include standard pre-defined files as well as we can also include user-defined files.

  • Standard File :- These are the header files which are open and closed by angle brackets ‘<‘ and ‘>’.

Ex:- To use string functions in a program like strlen(), substr(), stoi() we need to include <string.h> header file.

Syntax :-                         #include <file_name>

where are the opening and closing angle brackets tells the compiler to look for the file in standard directory.

  • User-defined files :- Whenever we works on a project the code is very large, so we can divide the program into smaller parts using user-defined header files.

Syntax:-                      #include”file_name”

Conditional Compilation

To execute a specific portion of the program normally or to skip a specific portion conditional compilation is used. It starts with #indef keyword and ends with #endif keyword.

Syntax:-                  #indef macro_name

                                                    statements;

                                       #endif;

Thanks For Reading !!

Feel free to ask any doubt regarding this post in the comment section below. Stay tuned for more informative programming posts.

Learn more C++ tutorials:

Leave a Reply

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