C Preprocessor

The C Preprocessor is a tool that processes the source code before compilation. It handles tasks like file inclusion, macro processing, conditional compilation, and symbol substitution. Every line beginning with # is processed by the preprocessor.

What Does the Preprocessor Do?

Before your C program is compiled, the preprocessor:

Preprocessor Directives

Major preprocessor directives include:

1. #include Directive

Used to include header files into your program.

Example: #include

#include <stdio.h>

int main() {
    printf("Hello Preprocessor!");
    return 0;
}

2. #define Directive

#define PI 3.14
#define MAX 100

3. Macro with Arguments

#define SQUARE(x) (x * x)

#include <stdio.h>

int main() {
    printf("%d", SQUARE(5));
    return 0;
}

4. Conditional Compilation

Used to compile parts of code depending on conditions.

#include <stdio.h>

#define INDIA

int main() {

#ifdef INDIA
    printf("Indian Version");
#else
    printf("Global Version");
#endif

    return 0;
}

5. #undef Directive

Used to delete a defined macro.

#define RATE 10
#undef RATE

6. #pragma Directive

Used for compiler-specific features.

#pragma startup myStart
#pragma exit myEnd
⚠ Note: #pragma usage depends on compiler. Some pragmas may not work in all compilers.

Practice Questions

  1. What is a preprocessor?
  2. Explain macro substitution with an example.
  3. Differentiate between #include <file.h> and "file.h".
  4. What is conditional compilation?
  5. What does #pragma do?

Practice Task

Write a program using macros that: βœ” Defines PI βœ” Computes area of a circle βœ” Uses conditional compilation to print either β€œSMALL” or β€œLARGE” based on radius