C++ Preprocessors

In C++ At of any kind time a CPP program is compiled at that time the next steps are taken:

The pre-processor is a practicality program, which processesdistinct instructions that can be otherwise are written in a C/CPP program. These instructions can be comprise a library or else some distinct instructions to the compiler about some assuredexpressions used in the program.

Pre-processor directives start by ‘#’ character.

Dissimilar preprocessor directives create different tasks.

This sort has simply one directive, which is called #include. This insertion directive is used to take in files into the existing file. This can be used as like this:

#include <stdio.h> containsstdio.h from include
folder
#include <iostream> embraces cpp class library header
iostream
#include<my.cpp> contains my.cpp file from include
folder
#include “my.h” includes my.h file from present working
folder
#include “myfolder/abc.h” includes abc.h file from the myfolder which
is accessible in current working folder

Here, we can see that #include is used with two selections angular brackets (<>)plus inverted commas (“”). Once #include is written with <> it means we are originating to hold the files from the include path demarcated in the compiler sets which is generally the include folder from the compiler. When #include is written with “” it means we are initiating to take in a file: first the current folder is confirmed and if it is not set up there, then the include folder of the compiler is crisscrossed.

Example:

 

#include
#include "header.h"

int main() {
	
	printf("The variable from the header file: a = %d", a);
	return 0;
}

Then save the code underneath in a file called header.h .also place it in the similar directory as main.c:

#ifndef __mynamespace__
	int a = 10;
#endif

The variable declared in the header.h file will be comprised in main.c, so we can use the variable in main().

Concept of macros

These are used to describe macros, which are one, or more program statements and they are long-drawn-out inline. i.e. they are replaced at the place of macro call different functions in which the execution pointer transfers to the function definition.

Consider here two directives for Macro Definition:

  • #define – it is Used to definea macro
  • #undef – it is Used to undefinea macro

Example:

 

 

Here,we can understand,

  • We do not dismiss the macros using (;)
  • The Macros be able to be parameterized
  • Macros can be reliant onon other macros i.e.need be defined before they are used.
  • Macros are usually written in capital letters to escapemisperception between macro and functions.
  • Parameters are bounded in () to avoid the vagueness arising due to operator precedence.
  • Directives are useful whenever things are inclined toto changes and are used at numerous places in one program. Altering the directive value at the top will grounds the consistent changes at all the spaces in the program.

If the directive needs use of more than one line then it can be sustained in the next line subsequently placing ‘\’ at the end of the first line.

Example:

Leave A Comment

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