Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

What is the preprocessor in C++?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The preprocessor is a directive given to the compiler before the compilation phase of the program. The compiler executes these directives before compiling the code into an executable file.

Take a look at the image below:

The image above illustrates that, after the Preprocessing phase, all preprocessor directives are replaced with C++ code in the source code file. Then, the compilation begins.

Structure of a Preprocessor directive

A preprocessor directive begins with the # character – only white-space characters can come before a preprocessor directive on a line. The directives may have additional tokenscharacters separated by spaces after them on the line. For example:

#example_directive token1 token2

Common Preprocessor directives

The following are some preprocessor directives that beginner programmers encounter.

#include Preprocessor directive

The structure of the #include directive is:

#include filename

The filename is the name of a standard library (if it is enclosed in ankle brackets <>) or a custom C++ file (if it is enclosed in double quotes "").

When the compiler encounters the #include directive, it replaces the line with the contents of the file.

#define Preprocessor directive

The #define directive is used to define Macros and Symbolic Constants. The structure of the #define directive is:

#define identifier replacement-text

In the preprocessing phase, the compiler replaces any occurrences of identifier that don’t occur as part of a string literaltext enclosed in double quotes in the source code or a commenttext in the source code that is ignored by the compiler with the replacement-text.

#error Preprocessor directive

The structure of the #error directive is:

#error tokens

Here, the tokens are characters separated by white-spaces.

In the preprocessing phase, if the compiler encounters the #error preprocessor directive, it terminates the compilation and prints the tokens as error on the standard output.

Conditional inclusion Preprocessor directives

Conditional inclusion Preprocessor directives consist of the following:

  • #if
  • #ifdef
  • #ifndef
  • #elif
  • #else
  • #endif

These directives instruct the compiler to conditionally include parts of the source code for compilation. The following code provides an example:

#include <iostream>
using namespace std;
#define PI 3.142
int main() {
#ifdef PI
cout << "The value of PI is: " << PI << endl;
#else
cout << "PI is not defined!" << endl;
#endif
return 0;
}

The code above outputs The value of PI is: 3.142 because #ifdef PI evaluates to true. This happens since PI is defined as 3.142 on line 4.

Note: line 10 does not get compiled because the #else clause is ignored by the compiler when #ifdef PI evaluates to true.

RELATED TAGS

c++

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring