Preprocessor directives
Preprocessor Directives The preprocessor is a hidden layer of the compiler that executes before the compiler itself begins working. It performs a set of...
Preprocessor Directives The preprocessor is a hidden layer of the compiler that executes before the compiler itself begins working. It performs a set of...
The preprocessor is a hidden layer of the compiler that executes before the compiler itself begins working. It performs a set of tasks on the source code, including:
Replacing macros: Macros are special variables defined using the # symbol, which are replaced with their actual values before the compiler reads the code.
Handling conditional directives: The if, else, and switch keywords are used to control the flow of the program based on specific conditions.
Defining function prototypes: These are used to declare functions without invoking them, allowing them to be used later.
Importing external code: Preprocessor can import code from other source files, allowing developers to build complex projects with multiple components.
These directives help the compiler understand the program's structure and how to interpret the source code before the actual compiler starts working. This allows for efficient processing and faster execution of the program.
Examples:
cpp
#define PI 3.141592653;
cpp
if (age > 18) {
// Code to be executed if age is greater than 18
}
cpp
void say_hello() {
std::cout << "Hello world!" << std::endl;
}
cpp
#include "other_file.h"
int main() {
// code from other_file.h
}