Lesson 26: Preprocessor Directives (Part 1)
The preprocessor is the first phase of compilation. It reads the source code and performs certain text substitutions before the actual compiler sees the code. Directives start with the # symbol.
1. #include Directive
Used to include the content of another file (usually a header file) into the current file.
a) Standard Library Headers
Uses angle brackets (<>) to search predefined system directories.
c #include <stdio.h> #include <math.h>
b) User-Defined Headers
Uses double quotes ("") to search the current source directory first.
c #include "my_utility.h"
2. #define Directive (Macros)
Used to define symbolic constants (macros). Wherever the identifier is used in the code, the preprocessor replaces it with the substitution text.
a) Defining Simple Constants
c #define MAX_SIZE 100
int array[MAX_SIZE]; // Replaced by int array[100];
b) Defining Function-like Macros
Macros can accept arguments. They perform text substitution, not function calls (avoiding function call overhead).
c // WARNING: Macros are simple text substitutions and can lead to side effects! #define SQUARE(x) (x * x)
int y = SQUARE(5); // y = (5 * 5) -> 25
// Bad example: SQUARE(a++) becomes (a++ * a++), executing a++ twice.
Best Practice: Always parenthesize macro arguments and the definition itself to prevent precedence issues.