
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How a Preprocessor works in C/C++?
Here we will see how the preprocessors are working in C or C++. Let us see what are the preprocessors.
The preprocessors are the directives, which give instructions to the compiler to preprocess the information before actual compilation starts.
All preprocessor directives begin with #, and only white-space characters may appear before a preprocessor directive on a line. Preprocessor directives are not C++ statements, so they do not end in a semicolon (;).
You already have seen a #include directive in all the examples. This macro is used to include a header file into the source file.
There are number of preprocessor directives supported by C++ like #include, #define, #if, #else, #line, etc. Let us see important directives −
The #define preprocessor
The #define preprocessor directive creates symbolic constants. The symbolic constant is called a macro and the general form of the directive is −
#define macro-name replacement-text
Example
#include <iostream> using namespace std; #define PI 3.14159 int main () { cout << "Value of PI :" << PI << endl; return 0; }
Output
Value of PI :3.14159
Conditional Compilations
There are several directives, which can be used to compile selective portions of your program's source code.This process is called conditional compilation.
The conditional preprocessor construct is much like the ‘if’ selection structure. Consider the following preprocessor code −
#ifndef NULL #define NULL 0 #endif
You can compile a program for debugging purpose. You can also turn on or off the debugging using a single macro as follows −
#ifdef DEBUG cerr <<"Variable x = " << x << endl; #endif
This causes the cerr statement to be compiled in the program if the symbolic constant DEBUG has been defined before directive #ifdef DEBUG. You can use #if 0 statment to comment out a portion of the program as follows −
#if 0 code prevented from compiling #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
Trace: Inside main function The minimum is 30 Trace: Coming out of main function
- Related Articles
- C/C++ Preprocessor Directives
- What is the #define Preprocessor in C++?
- Convert C/C++ program to Preprocessor code\n
- Explain how a rocket works.
- How Region of Interest (ROI) works in OpenCV using C++?
- How Node.js works in background - A brief analysis
- How JVM works?
- How Web Works?
- How scriptblock works in PowerShell?
- How "getElementByID" works in JavaScript?
- How EXPORT_SET() function works in MySQL?
- How Ternary operator in PowerShell Works?
- How optional chaining works in TypeScript?
- How Optimization in Machine Learning Works?
- In MySQL, how IN() comparison function works?
