⬅ Previous Next ➡

Preprocessor & Modular Programming

Preprocessor & Modular Programming in C
  • C Preprocessor runs before compilation and processes directives that start with #.
  • Preprocessor is used for macros, header files, conditional compilation, and code reuse.
  • Modular programming means dividing a program into smaller files (modules) using header (.h) and source (.c) files.
  • Inline functions reduce function call overhead for small functions.

1) C Preprocessor Directives

  • #include : includes header files.
  • #define : defines macros/constants.
  • #undef : removes a macro definition.
  • #if, #elif, #else, #endif : conditional compilation.
  • #ifdef, #ifndef : compile code based on macro defined or not.
  • #pragma : compiler-specific instructions.
#include <stdio.h>
#define PI 3.14159
#define MSG "Welcome to C"

int main() {
    printf("%s\n", MSG);
    printf("PI = %.5f\n", PI);
    return 0;
}

2) Macros

  • Object-like macro: constant replacement.
  • Function-like macro: behaves like a function (use parentheses to avoid errors).
#include <stdio.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define SQUARE(x) ((x) * (x))

int main() {
    int x = 10, y = 25;

    printf("MAX = %d\n", MAX(x, y));
    printf("SQUARE(5) = %d\n", SQUARE(5));

    return 0;
}

3) Conditional Compilation

  • Useful to enable/disable debug code and support different platforms.
#include <stdio.h>
#define DEBUG 1

int main() {

#if DEBUG
    printf("Debug mode ON\n");
#else
    printf("Debug mode OFF\n");
#endif

    return 0;
}

4) Header Files

  • Header file (.h) contains function prototypes, macros, and declarations.
  • Types:
    • System headers: <stdio.h>, <stdlib.h> (use < >)
    • User headers: "myfile.h" (use quotes)
  • Use include guards to avoid multiple inclusion.
// mymath.h
#ifndef MYMATH_H
#define MYMATH_H

int add(int a, int b);

#endif
// mymath.c
#include "mymath.h"

int add(int a, int b) {
    return a + b;
}
// main.c
#include <stdio.h>
#include "mymath.h"

int main() {
    printf("Sum = %d\n", add(10, 20));
    return 0;
}

5) Modular Programming Concepts

  • Split program into multiple files:
    • .h for declarations (what is available)
    • .c for definitions (how it works)
  • Benefits:
    • Easy maintenance and debugging
    • Code reusability
    • Team development becomes easier

6) Inline Functions

  • inline suggests compiler to replace function call with function body (for small functions).
  • It can improve speed but may increase program size.
  • Mostly used in headers for small utility functions.
#include <stdio.h>

static inline int cube(int x) {
    return x * x * x;
}

int main() {
    printf("Cube of 3 = %d\n", cube(3));
    return 0;
}

7) Compilation of Multiple Files

  • Compile and link:
    • gcc main.c mymath.c -o app
    • ./app

8) Quick Notes

  • Preprocessor directives start with #.
  • Macros are replaced before compilation (no type checking).
  • Header files store prototypes and declarations.
  • Modular programming makes large programs manageable.
  • Inline functions can make small repeated operations faster.
⬅ Previous Next ➡