Preprocessor Directives in C++

Preprocessor directives (instructions) are the special lines of code that are normally placed at the top of C++ program. These diectives are always starts from # symbol. Like other C++ statements there is no semicolon is used after these instructions. Preprocessor directives are used approximately in every small and large program. The most commonly used preprocessor directive is include directive. This is used to include library files in the source program. Which are also called header files.

There are many preprocessor directives supported by C++. Some commonly used are provided as.

List of Preprocessor Directives

No Preprocessor Directive Use
1 #include include directive is used to include header/library files in source program.
2 #define define directive is used to define symbolic constants/macros in program.
3 #ifdef Checks if particular macro/directive is defined.
4 #ifndef Checks whether particular macro/directive is not defined.
5 #if include piece of code in source program if provided expression is true.
6 #endif It is the part of #if directive.
7 #else It is used as an alternative of #if directive
8 #elif Any number of #eilf directives can be used in between #if and #endif directives
9 #undef it is used to clear/undefine already defined preprocessor directive
10 #error This is error directive. It is used to abort the compilation process from where it is ues.

Preprocessor directives are combined/liked in source program by Pre-Processor. At the start of compilation prerocessor directives are included in the source program.

1. #include directive

#include directive is used to include external library files in the source program. This directive is normally used in every C++ program. Include directive is written at the top of program.

#include Directive Example

#include <iostream>
using namespace std;

int main() {

cout<<"Start Learning Preprocessor Directives";

return 0;
}

2. #define directive

#define directive is used to define symbolic constants/macros with or without arguments. The compiler replaces the symbolic constant in entire lines of code with the value of this constant.

#define Directives without Arguments

#define directive is used to define macro/symbolic constants in program.

#include <iostream>
using namespace std;

int main() {
  #define MAX_LIMIT 500

  cout<<MAX_LIMIT;

  return 0;
}

#define directives/Macros also accepts arguments/parameters as input and can return result after some manipulation. Therefore macros can also be used in the same way as functions.

#define Directive with Arguments

#include <iostream>
using namespace std; 
// define directive with arguments 
#define SUM(x, y) (x + y) 
int main() 
{ 
    int a = 5;
    int b = 15;
    int total; 
  
    total= SUM(a, b); 
  
    cout<<"Sum of: "<<a<<" and "<<b<<"is "<<total; 
  
    return 0; 
} 

3. #ifdef directive

#ifdef directive is used as a selection structure. It checks whether a directive/macro is defined or not. If provided macro is defined then it will return true else will return false. This is used for conditional compilation. Commonly #ifdef is used to include Platform specific code. #ifdef directive must close with #endif directive.

#ifdef Directive Example

#include <iostream>
using namespace std;
#define CAPACITY 100

int main()
{
   #ifdef CAPACITY
   cout<<"Welcome To Institute Management System";
   #endif

   return 0;
}

4. #ifndef directive

#ifndef is reciprocal of #ifdef preprocessor directive. It stands for If Not Defined. It return true if the provided directive is not defined. It is also used to in conditional, platform specific source code in program.

#ifndef Directive Example

#include <iostream>
using namespace std;

int main()
{
   #ifndef CAPACITY
   cout<<"Welcome To Institute Management System";
   #endif
   
   return 0;
}

The above code will print Welcome To Institute Management System on screen because CAPACITY directive is not define in this code.

5. #if directive

#if directive is also used to include/exclude conditional base code.

#if Directive Example

#include <iostream>
using namespace std;

#define CAPACITY 100

#if CAPACITY == 100

#undef CAPACITY

#define CAPACITY 200

#endif


int main()
{
   cout<<"Capacity define directive value is: "<<CAPACITY;
   return 0;
}

6. #endif directive

#endif directive is used as a part of #if directive. It indicates the end point of #if directive. As used in the above example.

7. #else directive

#else directive also used with #if and #endif directives. It will include lines of code when #if directive returns false.

#else Directive Example

#include <iostream>
using namespace std;

#define CAPACITY 100

#if CAPACITY == 200

#undef CAPACITY

#else

#undef CAPACITY

#define CAPACITY 400

#endif

int main()
{
   cout<<"Capacity define directive value is: "<<CAPACITY;
   return 0;
}

In the above piece of code #if expression is false so #else directive will add provided alternate piece of code.

8. #elif directive

#elif directive can be used in between in #if and #endif directives any number of times. It stands for else if. If any expression of #elif returns true then remaining #elif will be skipped.

#elif Directive Example

#include <iostream>
using namespace std;

#define CAPACITY 100
#define LIMIT 20

#if CAPACITY == 200
#undef CAPACITY

#elif CAPACITY == 100
#undef CAPACITY
#define CAPACITY 500

#endif

int main()
{
   cout<<"Capacity define directive value is: "<<CAPACITY;
   return 0;
}

9. #undef directive

#undef directive is used to remove any already defined directive.

#undef Directive Example

#include <iostream>
using namespace std;

#define CAPACITY 100

#undef CAPACITY

#define CAPACITY 500

int main()
{
   cout<<"Capacity define directive value is: "<<CAPACITY;
   return 0;
}

In above program #undef is used to clear defination of CAPACITY and then redefine it using #define statement.

10. #error directive

This directive is used to terminate compilation with specified ERROR MESSAGE. It is normally used with #if directive.

#error Directive Example

#include<iostream>
using namespace std;

#define IMS 100

#ifdef IMS

#error IMS is not here

#endif

int main() {
   cout<<"Welcome To IMS";

   return 0;
}

 

Comments
Login to TRACK of Comments.