C++ Preprocessor3 min read

As you have studied in C preprocessor before, C++ preprocessor also has the same exact concept of it with a change in coding.

As the “pre” means beforehand, similarly it means processing something before passing it on further. The preprocessor is a program that processes the source program before it is passed to the compiler. So we can say that it is a separate process in the compilation.

Preprocessor gives specific features called directives. All preprocessor directive begins with# symbol and do not need to be end with senicolon (;). We have seen the use of #include in the program till now.

The following are preprocessor directives:

  • Macro expansion
  • File inclusion
  • Conditional Compilation
  • Miscellaneous directives

Macro Definition

macro is defined by #define in a program. It is used to define some constant in a program that can be used anywhere during the entire program. This constant becomes global value.

#define PI 3.1415

This statement is called ‘macro definition’ or more commonly, just a ‘macro’.
Its purpose: during preprocessing, the preprocessor replaces every occurrence of PI in the program with a 3.1415 value.

You may try this program:

Area of a circle :19.6349


File inclusion

#include is used to include some files into the program. These are usually header files, but sometimes maybe any text file. The command looks something like this:

#include <iostream>

iostream is the header file that contains the library function useful to the program.


Conditional Compilation

The C++ preprocessor provides a series of directives for conditional compilation: #if, #elif, #else, #ifdef, #ifndef, and #endif. These commands cause the preprocessor to include or exclude sections of the source code from compilation depending on certain conditions.

Conditional compilation is used for three main purposes:

  • Optionally include debugging code
  • Enclose non-portable code
  • Guard against multiple inclusion of header files.

Syntax:

#ifdef, #endif, #if, #else, #ifndef

#ifndef

It tells the C Preprocessor to define TEXT only if TEXT isn’t already defined.

#ifdef Directive

You can also add nested conditional to your #if...#else using #elif


Other directives

#undef is used to undefine a defined macro variable.

It tells the C Preprocessor to undefine existing FILE_SIZE and define it as 38.

#pragma startup and #pragma exit: These are used to call a function before and after the main function in a C++ program.

#undef, #pragma startup, #pragma exit


Predefined Macros

MacroValue
__DATE__A string containing the current date
__FILE__A string containing the file name
__LINE__An integer representing the current line number
__TIME__A string containing the current date.

MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More