When we compile our project, we might think it is directly getting converted to binary code. Instead, prior to compilation each code (.cpp) file goes through a preprocessing phase. In this phase, a program called the preprocessor makes various changes to the text of the code file. The preprocessor does not actually changes to the text of the in any way – rather, all changes made by the preprocessor happen either temporarily in-memory or using temporary files.
What is Preprocessing
Preprocessing is the initial phase of the C++ Compilation process. It involves a set of activities performed by the preprocessor, a part of the compiler responsible for handling directives and manipulating the source code before it is sent to the actual compiler. The preprocessor operates on the source code before any compilation takes place.
What is the Preprocessor Directives
Preprocessor directives are commands that guide the preprocessor in modifying the source code before it undergoes compilation. They are identified by the #
symbol and are not terminated with a newline not a semicolon.
What is Preprocessor
The preprocessor is a component of the C++ compiler that performs various tasks on the source code before it is actually compiled. Its main purpose is to manipulate the source code before the compiler takes over. The preprocessor is responsible for handling preprocessing directives, which are special commands denoted by the #
symbol.
Here are some key tasks performed by the preprocessor:
- File Inclusion: The
#include
directive is used to include the contents of a file within another file. This is commonly used for including header files that declare functions, classes, or constants used in the program. - Macro Expansion: The
#define
directive is used to define macros, which are symbolic names representing a sequence of code. During preprocessing, these macros are replaced with their corresponding code. - Conditional Compilation: Directives like
#ifdef
,#ifndef
,#else
, and#endif
are used for conditional compilation. They allow parts of the code to be included or excluded based on predefined conditions. - Removal of Comments: The preprocessor removes comments from the source code. Comments are ignored during the actual compilation but provide essential documentation for programmers.