Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

The Preprocessor: Macros & Directives

Lesson 28 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

The Silent Assistant

The Preprocessor is a program that processes your source code before it ever reaches the compiler. It looks for lines starting with the hash symbol (#).

1. File Inclusion (#include)

  • #include <stdio.h>: Tells the preprocessor to look in the system's standard library folders.
  • #include "my_header.h": Tells it to look in your project's current folder first.

2. Macros (#define)

This is a simple "Search and Replace" tool.

#define PI 3.14159
#define SQUARE(x) ((x) * (x))

Why are parentheses needed in macros? Because macros are literal text replacements. If you define #define MULT(a, b) a b, then MULT(1+1, 2) becomes 1+1 2, which is 3. But if you use ((a)(b)), it becomes ((1+1)(2)), which is 4.

3. Conditional Compilation

Allows you to include or exclude blocks of code based on specific conditions. This is how professional developers write one codebase that works on Windows, Mac, and Linux.

#ifdef _WIN32
    // Code that only runs on Windows
#elif defined(__linux__)
    // Code that only runs on Linux
#endif

4. Header Guards (Vital for Large Projects)

If two files both include the same header, you'll get a "Redefinition Error." Header guards prevent this.

#ifndef MY_TOOLS_H
#define MY_HEADER_H
// All your functions and structs here
#endif

5. Predefined Macros

C provides several useful macros automatically:

  • DATE: The current date as a string.
  • TIME: The current time.
  • FILE: The name of the current file.
  • LINE: The current line number (great for error logging!).
While macros are powerful, they are "blind." They don't understand C types or logic. Overusing complex macros can make your code very difficult to debug because the errors the compiler sees are in the expanded text, not in what you actually wrote!