Pre-Processors in C Language – BKNMU Junagadh | BCA Sem 1 Notes



BCA Sem 1  |  Unit 1  |  Computer Concepts & C Programming

Pre-Processors in C Language — BKNMU Junagadh

Macros, File Inclusion, Conditional Compilation, and Preprocessor Directives (#)

BKNMU Junagadh NEP 2020 C Preprocessor #include & #define Macros
In simple words: The C Preprocessor is a system tool that processes your source code before it is passed to the actual compiler. It scans the code for lines starting with a hash symbol (#), known as Preprocessor Directives, and performs text substitution, file insertion, and code filtering.
🎨 Where Preprocessing Happens in C Compilation

The 4 Stages of C Program Execution:

1. Source Code (.c) 2. Preprocessor (expands #) 3. Compiler (.obj/.o) 4. Linker (.exe)

The preprocessor outputs expanded C code where all #include files are pasted, comments removed, and all #define macros replaced.

📖 Four Major Types of Preprocessor Directives

C directives are broadly classified into four categories based on their function:

#include

1. File Inclusion

Inserts contents of external header files or user C files directly into the program before compiling.

#define / #undef

2. Macro Substitution

Defines symbolic constants or function-like macros for textual replacement throughout the program.

#ifdef / #ifndef

3. Conditional Compilation

Compiles or skips specific portions of code based on pre-defined conditions or macro flags.

#pragma / #error

4. Miscellaneous

Passes compiler-specific commands or halts compilation with custom error messages.

1️⃣ Comprehensive Table of Preprocessor Directives

Here is a detailed breakdown of common preprocessor directives used in C:

DirectiveCategoryDescription & PurposeExample Syntax
#include <file.h>File InclusionSearches standard system directories for system header files like stdio.h or math.h.#include <stdio.h>
#include "file.h"File InclusionSearches the current project directory first for custom header files created by the programmer.#include "myheader.h"
#defineMacro SubstitutionDefines a macro name (symbolic constant or function-like macro) for automatic text replacement.#define PI 3.14159
#undefMacro SubstitutionUndefines an existing macro, restricting its usage in subsequent parts of the code.#undef PI
#ifdefConditionalChecks if a macro has been defined using #define; executes code block if true.#ifdef DEBUG
#ifndefConditionalChecks if a macro has NOT been defined; essential for preventing double-header inclusion.#ifndef HEADER_H
#endifConditionalMarks the end of a conditional preprocessor block started by #if, #ifdef, or #ifndef.#endif
#errorMiscellaneousAborts compilation immediately and outputs a user-specified diagnostic error message.#error "Unsupported OS"
ℹ️ Crucial Syntax Rule: Preprocessor directives do NOT end with a semicolon (;)! Writing #define PI 3.14; will cause compilation errors because the compiler replaces PI with 3.14; everywhere in your code.
💻 Code Sample — Macros and Conditional Compilation

Observe how preprocessor macros and conditional checks work in C code:

#include <stdio.h> // 1. Symbolic Constant Macro #define LIMIT 5 // 2. Function-like Macro #define SQUARE(x) ((x) * (x)) // 3. Conditional Compilation Flag #define DEBUG_MODE int main() { int num = 4; printf("Square of %d: %d\n", num, SQUARE(num)); #ifdef DEBUG_MODE printf("DEBUG: Program running with limit %d\n", LIMIT); #endif return 0; }
💡 Function-like Macro Tip: Always enclose parameters in parentheses inside function-like macros (e.g., ((x) * (x))). Writing #define SQUARE(x) x * x will cause incorrect calculation order when evaluated as SQUARE(2 + 3)!
🔍 System Headers vs. User-Defined Headers

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 .c file resides first before checking system folders.
🔄 Key Advantages of Preprocessors
1

1. Code Readability & Maintainability

Using symbolic constants like #define MAX_STUDENTS 100 allows you to update values in one central place.

2

2. Faster Execution with Macros

Function-like macros eliminate function call overhead during runtime since substitution happens before compilation.

3

3. Cross-Platform Portability

Conditional directives like #ifdef _WIN32 allow identical source code to compile seamlessly on Windows, Linux, or macOS.

⚠️ Common Pitfall: Preprocessor macros perform **plain text substitution** without type checking! They do not allocate memory or hold variables like const variables do.
📝 Quick Revision — Key Exam Points
  • 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: #define creates symbolic constants or inline function-like expansions.
  • Conditional Directives: #ifdef, #ifndef, #else, #endif allow selective code compilation.

Post a Comment

Thanks for comment.

Previous Post Next Post