Preprocessor Directives

Understanding C Program Compilation Process

Introduction to Preprocessor (प्रीप्रोसेसर का परिचय)

Preprocessor C program को compile करने से पहले उस पर कुछ operations perform करता है। ये source code को modify करता है और compiler को modified code pass करता है।

The preprocessor performs certain operations on a C program before compilation. It modifies the source code and passes the modified code to the compiler.

Key Points (मुख्य बिंदु):

  • Preprocessor directives # से start होते हैं
  • Preprocessor directives start with #
  • Preprocessing compilation से पहले होता है
  • Preprocessing occurs before compilation
  • Preprocessor code को modify करता है
  • Preprocessor modifies the code
Source Code
Preprocessing
Compilation

Common Directives (कॉमन डायरेक्टिव्स)

Common preprocessor directives और उनका use:

Common preprocessor directives and their usage:

#include       // Include header file
#define PI 3.14159     // Define constant
#define MAX(a,b) ((a)>(b)?(a):(b))  // Define macro

#ifdef DEBUG           // Conditional compilation
    printf("Debug mode\n");
#endif

#pragma once          // Prevent multiple inclusion
Directive (डायरेक्टिव) Description (विवरण)
#include Header file को include करता है
#include Includes a header file
#define Constant या macro को define करता है
#define Defines a constant or macro

Macros (मैक्रोस)

Macros और उनका implementation:

Macros and their implementation:

// Simple macro
#define SQUARE(x) ((x) * (x))

// Multi-line macro
#define PRINT_ARRAY(arr, size) \
    for(int i=0; i

Macro Features (मैक्रो फीचर्स):

  • Stringizing operator (#) string बनाता है
  • Stringizing operator (#) creates a string
  • Token pasting operator (##) tokens को join करता है
  • Token pasting operator (##) joins tokens
  • Macros में parentheses का use important है
  • Parentheses are important in macros

Conditional Compilation (कंडीशनल कंपाइलेशन)

Conditional compilation directives:

Conditional compilation directives:

#ifdef DEBUG
    // Debug code
    printf("Debug information\n");
#else
    // Release code
    printf("Release version\n");
#endif

#if VERSION > 2
    // New version features
#elif VERSION == 2
    // Version 2 features
#else
    // Old version features
#endif

Best Practices (सर्वोत्तम प्रथाएं)

Preprocessor directives के best practices:

Best practices for preprocessor directives:

  • Macro Names: UPPERCASE में macro names use करें
  • Macro Names: Use UPPERCASE for macro names
  • Parentheses: Macro definitions में parentheses का use करें
  • Parentheses: Use parentheses in macro definitions
  • Header Guards: Header files में include guards use करें
  • Header Guards: Use include guards in header files
  • Conditional Compilation: Debug और release versions के लिए use करें
  • Conditional Compilation: Use for debug and release versions