- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Preprocessor
Preprocessors in C++, are basically the directives, instructs the C++ compiler to pre-process the information before the actual program compilation.
#define Preprocessor in C++
Macros are built on the #define preprocessor. Normally a #define would look like :
#define PI 3.142
But, a macro would look like this (also called as function macro).
#define SQUARE(x) x∗x
The main difference is that the first example is a symbolic constant and the second is an expression. If the macro above was used in some code it may look like this :
/* C++ Preprocessor - Example Program */ #include<iostream.h> #include<conio.h> #define SQUARE(x) x*x void main() { clrscr(); int val; cout<<"Enter a number: "; cin>>val; cout<<"Square = "<<SQUARE(val); getch(); }
After processing the code would become :
void main() { clrscr(); int val; cout<<"Enter a number: "; cin>>val; cout<<"Square = "<<val*val; getch(); }
Here is the sample run of this C++ program:

The text replacement for a macro, is known as macro expansion.
A few things that you must know about macros are :
- A macro without argument is treated like a symbolic constant.
- A macro with arguments has its arguments substituted for replacement text, when the macro is expanded.
- A macro substitutes text only; it does not check for data types.
While defining macros, make sure to use parenthesis, as it ensures the correct result. For instance, if you have defined a macro like :
#define CIRCLE_AREA(x) PI∗x∗x
and you are using it as :
area = CIRCLE_AREA(c + 2) ;
then it would be expanded as :
area = 3.14159 ∗ c + 2 ∗ c + 2 ;
See, would it yield the correct result? To obtain the correct result, you should define this macro as :
#define CIRCLE_AREA((x)) PI∗(x)∗(x)
would cause
rectArea = RECTANGLE_AREA(x + 4, y +7) ;
be expanded as
rectArea = ((x+4) ∗ (y+7)) ;
Here is an example, illustrating the #define preprocessor in C++
/* C++ Preprocessor - Example Program */ #include<iostream.h> #include<conio.h> #define PI 3.14 void main() { clrscr(); cout<<"Value of PI is "<<PI; getch(); }
Here is the sample output of the above C++ program:

Let's take one more example, demonstrating preprocessor in C++
/* C++ Preprocessor - Example Program */ #include<iostream.h> #include<conio.h> #define PI 3.14 void main() { clrscr(); int rad; cout<<"Enter radius of the circle: "; cin>>rad; cout<<"Area of the circle is "<<PI*rad*rad; getch(); }
Here is the sample run of the above C++ program:

For more detail about #define, refer C++ #define
Function Macros in C++
Here is an example program demonstrating the function macros (preprocessor) in C++
/* C++ Preprocessors - Example Program */ #include<iostream.h> #include<conio.h> #define MIN(num1, num2) (((num1)<(num2))?num1:num2) void main() { clrscr(); int a, b; cout<<"Enter any two number: "; cin>>a>>b; cout<<"Minimum of the two number is "<<MIN(a, b); getch(); }
Below is the sample run of the above C++ program:

Here is one more example program, also demonstrating preprocessor in C++
/* C++ Preprocessors - Example Program */ #include<iostream.h> #include<conio.h> #define MAX(num1, num2) (((num1)>(num2))?num1:num2) void main() { clrscr(); int a, b; cout<<"Enter any two number: "; cin>>a>>b; cout<<"Maximum of the two number = "<<MAX(a, b); getch(); }
Below is the sample run of this C++ program:

Conditional Compilation in C++
Here is an example program, demonstrating the conditional compilation in C++:
/* C++ Preprocessor - Example Program */ #include<iostream.h> #include<conio.h> #define DEBUG #define MIN(num1, num2) (((num1)<(num2))?num1:num2) void main() { clrscr(); int a, b; cout<<"Enter two number: "; cin>>a>>b; #ifdef DEBUG cerr<<"Trace: inside the main() function\n"; #endif #if 0 cout<<MKSTR(HELLO C++)<<endl; #endif cout<<"The minimum = "<<MIN(a, b)<<"\n"; #ifdef DEBUG cerr<<"Trace: coming out of the main() function\n"; #endif getch(); }
Here is the sample run of the above C++ program:

Below is another example program, also demonstrating the conditional compilation in C++
/* C++ Preprocessor - Example Program */ #include<iostream.h> #include<conio.h> #define DEBUG #define MAX(num1, num2) (((num1)>(num2))?num1:num2) void main() { clrscr(); int a, b; cout<<"Enter two number: "; cin>>a>>b; #ifdef DEBUG cerr<<"Trace: inside the main() function\n"; #endif #if 0 // this is commented part cout<<MKSTR(HELLO C++)<<endl; #endif cout<<"The maximum of the two number = "<<MAX(a, b)<<"\n"; #ifdef DEBUG cerr<<"Trace: coming out of the main() function\n"; #endif getch(); }
Following is the sample run of the above C++ program:

# and ## Operators in C++
The # operator causes a replacement-text token to be converted to a string surrounded by the quotes. Here is an example demonstrating this.
/* C++ Preprocessor - Example Program */ #include<iostream.h> #include<conio.h> #define MKSTR(z) #z void main() { clrscr(); cout<<MKSTR(HELLO C++)<<endl; getch(); }
Following is the sample output of the above C++ program:

Here the line:
cout<<MKSTR(HELLO C++)<<endl;
becomes
cout<<"HELLO C++"<<endl;
The ## operator is used to concatenate two tokens. Below is an example program demonstrating this concept:
/* C++ Preprocessor - Example Program */ #include<iostream.h> #include<conio.h> #define concat(i, j) i##j void main() { clrscr(); int ab=100; cout<<concat(a, b); getch(); }
Here is the sample output of this C++ program:

Here the line
cout<<concat(a, b);
becomes
cout<<ab;
« Previous Tutorial Next Tutorial »