Preprocessor Directive in C++ Programming


Preprocessor directives are lines included in the code of programs preceded by a hash sign ( # ). The preprocessors are the directives, which give instructions to the compiler to preprocess the information before actual compilation starts. Preprocessing directives are lines in your program that start with # . The # is followed by an identifier that is the directive name. The Whitespace is also allowed before and after the #.

  • The first two lines define constants using the #define preprocessor directive. The constant PI is set to 3.14, and rectangle() is defined using the length and breadth parameters.
  • The createString() macro uses the # operator to convert the argument to a string literal.
  • The concat() macro uses the ## operator to concatenate two arguments and form a single token.
  • The program then prints the area of a circle using the constant PI, the area of a rectangle using the rectangle() macro, and the new string generated by the createString() macro.
  • The program also prints the value of concat(a,b) which is formed by concatenating the two characters 'a' and 'b'. Additionally, it prints out some special predefined macros such as __LINE__, __FILE__, __DATE__, __TIME__, and __cplusplus__.

Source Code

#include <iostream>
#define PI 3.14
#define rectangle(length, breadth) (length * breadth)
#define createString(s) #s
#define concat(a, b) a ## b
using namespace std;
//Preprocessor Directive in C++ Programming
int main()
{
    cout<<"Area of a circle : "<<PI*5*5<<endl;
    int length = 20, breadth = 5, area;
    area=rectangle(length,breadth);
    cout << "Area of a rectangle is: " << area<<endl;
    cout<<"New String : "<<createString(Tutor Joes)<<endl;
    int ab=100;
    cout<<"The Value of AB : "<<concat(a,b)<<endl;
    cout<<"__LINE__ :" << __LINE__ << endl;
    cout<<"__FILE__ :" << __FILE__ << endl;
    cout<<"__DATE__ :" << __DATE__ << endl;
    cout<<"__TIME__ :" << __TIME__ << endl;
    cout<<"__cplusplus:"<<__cplusplus<<endl;
 
    return 0;
 
}
 

Output

Area of a circle : 78.5
Area of a rectangle is: 100
New String : Tutor Joes
The Value of AB : 100
__LINE__ :17
__FILE__ :C:\Users\Tutor Joes\Desktop\Sandhiya\sample.cpp
__DATE__ :Mar 18 2022
__TIME__ :16:21:54
__cplusplus:201402
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples