Pre-Processors in C Language – Explained with Examples
Pre-processors in C are special instructions that are processed before the actual compilation of the program begins. They make C programs more flexible and manageable. In this post by BCA School, we’ll understand what pre-processors are, their types, and examples you can use in your practicals or exams.
🔹 What is a Pre-Processor?
A pre-processor is a program that processes the source code before compilation. In simple words, it handles all lines beginning with #
symbol, such as #include
, #define
, etc. These commands are known as pre-processor directives.
🔹 Types of Pre-Processor Directives
- 1. Macro Substitution Directives: Used to define constants or macros using
#define
. - 2. File Inclusion Directives: Used to include header files using
#include
. - 3. Conditional Compilation Directives: Used to compile specific portions of code using
#ifdef
,#endif
, etc. - 4. Miscellaneous Directives: Used for error handling or line control using
#error
and#line
.
🔹 Common Pre-Processor Directives in C
Directive | Purpose | Example |
---|---|---|
#define |
Defines a constant or macro | #define PI 3.14 |
#include |
Includes header files | #include <stdio.h> |
#ifdef / #endif |
Conditional compilation | #ifdef DEBUG |
#undef |
Undefines a macro | #undef PI |
🔹 Example Program
#include <stdio.h> #define PI 3.1416 #define AREA(r) (PI * r * r) int main() { float radius = 5.0; printf("Area of Circle = %.2f", AREA(radius)); return 0; }
🔹 Output
Area of Circle = 78.54
🔹 Key Points to Remember
- Pre-processors are not part of the compiler — they run before compilation.
- They begin with the
#
symbol and do not end with a semicolon. - They improve reusability and readability of programs.
🎯 Conclusion
Pre-processors in C play a vital role in simplifying complex programs and enhancing code efficiency. As a BCA student, understanding them helps in writing cleaner and more flexible code. Keep practicing these directives for better results in your C language practicals and viva!