Explain the pre-processor directives in C language

The preprocessor is a tool that processes the source code before it passes through the compiler. It works as an initial phase of compilation where it operates under the control of different command lines or directives.

Syntax

#directive_name [parameters]

Pre-processor Directives in C

Preprocessor directives are placed in the source program before the main function, begin with the symbol "#" in column one and do not require a semicolon at the end.

The commonly used preprocessor directives are −

  • #define
  • #undef
  • #include
  • #ifdef
  • #endif
  • #if
  • #else

The preprocessor directives are divided into three categories −

  • Macro substitution directives.
  • File inclusion directives.
  • Compiler control directives.

Macro Substitution Directives

This helps us to define macros that act as placeholders or shortcuts for code or constants and expressions. The preprocessor scans the source code and replaces each occurrence of macros with its corresponding text or value before compilation begins.

Syntax

#define identifier replacement_text

Example

Following is an example of different types of macros −

#include <stdio.h>

// Simple macro
#define MAX 500
#define PI 3.14159

// Function-like macro
#define SQUARE(x) ((x) * (x))

// Nested macro
#define A 10
#define B (A + 1)

int main() {
    printf("MAX value: %d<br>", MAX);
    printf("Value of PI: %.5f<br>", PI);
    printf("Square of 5: %d<br>", SQUARE(5));
    printf("Value of B: %d<br>", B);
    return 0;
}
MAX value: 500
Value of PI: 3.14159
Square of 5: 25
Value of B: 11

File Inclusion Directives

File inclusion is used for adding content of one file to another during preprocessing. This helps us when we need to include external libraries or our own header files.

Syntax

#include "filename" 
#include <filename>

Example

#include <stdio.h>
#include <math.h>

int main() {
    double num = 16.0;
    printf("Square root of %.1f is %.2f<br>", num, sqrt(num));
    return 0;
}
Square root of 16.0 is 4.00

File inclusion directive helps us to include files that contain declarations and function definitions. The angle brackets include standard library files, and double quotes are used for user-defined files.

Compiler Control Directives

These are used to control the compiler actions. C preprocessor offers a feature called conditional compilation, which can be used to switch on or off particular lines or groups of lines in a program based on conditions.

Example

Following example demonstrates conditional compilation −

#include <stdio.h>

#define DEBUG 1
#define VERSION 2

int main() {
    printf("Program started<br>");
    
    #ifdef DEBUG
        printf("Debug mode is ON<br>");
    #endif
    
    #if VERSION == 1
        printf("This is version 1<br>");
    #elif VERSION == 2
        printf("This is version 2<br>");
    #else
        printf("Unknown version<br>");
    #endif
    
    printf("Program finished<br>");
    return 0;
}
Program started
Debug mode is ON
This is version 2
Program finished

Key Points

  • Preprocessor directives are processed before compilation begins.
  • Use parentheses in function-like macros to avoid operator precedence issues.
  • Conditional compilation helps create platform-specific or debug-specific code.
  • File inclusion allows code reusability across multiple source files.

Conclusion

Preprocessor directives provide powerful tools for code optimization, conditional compilation, and file management. They enable efficient macro substitutions and help create flexible, maintainable C programs.

Aishwarya Naglot
Aishwarya Naglot

Writing clean code&hellip; when the bugs aren&rsquo;t looking.

Updated on: 2026-03-15T13:21:23+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements