BCA Sem 1 | Unit 1 | Computer Concepts & C Programming
Pre-Processors in C Language — BKNMU Junagadh
Macros, File Inclusion, Conditional Compilation, and Preprocessor Directives (#)
#), known as Preprocessor Directives, and performs text substitution, file insertion, and code filtering.
The 4 Stages of C Program Execution:
The preprocessor outputs expanded C code where all #include files are pasted, comments removed, and all #define macros replaced.
C directives are broadly classified into four categories based on their function:
1. File Inclusion
Inserts contents of external header files or user C files directly into the program before compiling.
2. Macro Substitution
Defines symbolic constants or function-like macros for textual replacement throughout the program.
3. Conditional Compilation
Compiles or skips specific portions of code based on pre-defined conditions or macro flags.
4. Miscellaneous
Passes compiler-specific commands or halts compilation with custom error messages.
Here is a detailed breakdown of common preprocessor directives used in C:
| Directive | Category | Description & Purpose | Example Syntax |
|---|---|---|---|
#include <file.h> | File Inclusion | Searches standard system directories for system header files like stdio.h or math.h. | #include <stdio.h> |
#include "file.h" | File Inclusion | Searches the current project directory first for custom header files created by the programmer. | #include "myheader.h" |
#define | Macro Substitution | Defines a macro name (symbolic constant or function-like macro) for automatic text replacement. | #define PI 3.14159 |
#undef | Macro Substitution | Undefines an existing macro, restricting its usage in subsequent parts of the code. | #undef PI |
#ifdef | Conditional | Checks if a macro has been defined using #define; executes code block if true. | #ifdef DEBUG |
#ifndef | Conditional | Checks if a macro has NOT been defined; essential for preventing double-header inclusion. | #ifndef HEADER_H |
#endif | Conditional | Marks the end of a conditional preprocessor block started by #if, #ifdef, or #ifndef. | #endif |
#error | Miscellaneous | Aborts compilation immediately and outputs a user-specified diagnostic error message. | #error "Unsupported OS" |
;)! Writing #define PI 3.14; will cause compilation errors because the compiler replaces PI with 3.14; everywhere in your code.
Observe how preprocessor macros and conditional checks work in C code:
((x) * (x))). Writing #define SQUARE(x) x * x will cause incorrect calculation order when evaluated as SQUARE(2 + 3)!
There is an important distinction between < > and " " in #include:
#include <filename.h>: Tells preprocessor to search standard library paths (e.g., GCC or Turbo C system include folders).#include "filename.h": Tells preprocessor to search the **current directory** where the source.cfile resides first before checking system folders.
1. Code Readability & Maintainability
Using symbolic constants like #define MAX_STUDENTS 100 allows you to update values in one central place.
2. Faster Execution with Macros
Function-like macros eliminate function call overhead during runtime since substitution happens before compilation.
3. Cross-Platform Portability
Conditional directives like #ifdef _WIN32 allow identical source code to compile seamlessly on Windows, Linux, or macOS.
const variables do.
- Definition: Preprocessors run before compilation to modify source code text.
- Syntax Indicator: All preprocessor directives start with the
#symbol. - No Semicolon: Directives do not end with semicolons.
- File Inclusion:
#include <file.h>for system headers;#include "file.h"for local headers. - Macros:
#definecreates symbolic constants or inline function-like expansions. - Conditional Directives:
#ifdef,#ifndef,#else,#endifallow selective code compilation.