Skip to content Skip to sidebar Skip to footer

Preprocessor Directives in C++

The compilation is an important phase of the software development cycle. The compilation is not a short task to perform and has many stages. The first stage is called preprocessing. The Preprocessor of the C++ programming language is a computer program and code used to perform different types of tasks before compiling the source code. . So, let’s get into detail.

What is Preprocessor or Preprocessing?

When we write a code, also known as source code, it gets processed by the compiler before getting compiled, and this technique is known as a preprocessor and this process preprocessing.

Although the technique comes under the compilation process, it is a different or separate method from the compiler, directing the compiler to preprocess the source code before compilation begins.

And the most important thing that one must remember is that its syntax begins with a ( # ) hashtag and does not require a semi-colon ( ; ) to end. The semi-colon is not required as preprocessor directives are not a statement in the c++

Syntax

#include <iostream>

Well, the Preprocessor does not end here. There are different preprocessor directives in c++.

Types of Pre-Processor Directives

C++ categorizes different types of preprocessor directives broadly into four categories, i.e.,

Inclusion Directives

 As the name suggests, this directive directs the compiler to include a file in the source code program. We use #include specifying the files included by inclusion directives in the source code program.

    SYNTAX

#include <iostream>

    Example

#include <iostream>    //Example of inclusion directive
int main(){

cout << "Welcome to GeekOnPeak" ;

}

Macro Definition Directives

 These are the pieces of code we give a name or a label to, so whenever we use the given name anywhere in our code, it substitutes with the code that the name holds. We define a macro by #define, and to undefine it, we use #undef.

Syntax

#define macro_name replacement_text

    Example

#include <iostream>
#define end 80            //Example of macra definition directives.
int main(){
for (int age; age < end; age++)
    {
              cout << age << "\n" ;
      } 
}

Conditional Compilation Directives

This help suppresses the compilation of some piece of code of the program based on some conditions. Some of these conditional compilation directives are:

  • #if: It tests a compile-time condition.
  • #elif: It comes after the #if.
  • #endif: It specifies the end of #if.
  • #ifdef: We use it to test for macro definition.
  • #ifndef: It tests whether a macro is not defined.
  • #else: It provides an alternative option when #if fails.

Example

#include <iostream>
#include <stdio.h>
#define NUM 2
using namespace std;

int main()
{
#if NUM != 2    // No error even when NUM is not defined.
       cout << "Macro NUM is defined to a non-zero value.";
#elif NUM != 1
       cout << "Macro NUM is defined not to be 1.";
   #endif
}
Output:

Macro NUM is defined not to be 1. 
Because the condition of #if is not valid, the value of NUM is 2. 

Other Directives

 This includes all the other directives we do not commonly use.

  • #error: It tells the Preprocessor to issue an error message and mark the source file as ill-formed.
  • #line: Supplies a line number for compiler messages
  • #pragma: It specifies implementation-defined instructions to the compiler

Example

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
#if NUM != 2    // No error even when NUM is not defined.
       cout << "Macro NUM is defined to a non-zero value.";
#elif NUM != 1
       cout << "Macro NUM is defined not to be 1.";
#endif
#error "Not to compile"
}

FOLLOW-UP

So that’s all about the Preprocessor in the C++ hope you’ve understood it well and are following the latest C++ blogs. Besides hope, you loved our new format of CTFU (code the fun up), and you enjoyed coding while learning as it focuses on more practical knowledge than theoretical knowledge.

Similarly, you must also know that GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more comprehensive and easy-to-grasp courses for you (for free). You can follow us on FacebookInstagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂

Leave a comment

Sign Up to Our Newsletter

Be the first to know the latest updates