Skip to content

Latest commit

 

History

History
78 lines (54 loc) · 1.42 KB

File metadata and controls

78 lines (54 loc) · 1.42 KB

Module 10: Preprocessor and Macros

Master the C preprocessor!

What You'll Learn

  1. Preprocessor directives
  2. Macros and macro functions
  3. Conditional compilation
  4. Header guards
  5. Common preprocessor tricks

Preprocessor Directives

#include <stdio.h>

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

int main() {
 printf("PI = %f\n", PI);
 printf("MAX(5, 10) = %d\n", MAX(5, 10));
 printf("SQUARE(4) = %d\n", SQUARE(4));
 return 0;
}

Header Guards

#ifndef MY_HEADER_H
#define MY_HEADER_H

// Header content here

#endif

Conditional Compilation

#define DEBUG

#ifdef DEBUG
 printf("Debug mode enabled\n");
#endif

#if defined(WINDOWS)
 // Windows code
#elif defined(LINUX)
 // Linux code
#else
 // Other OS code
#endif

Next Module

Module 11: Data Structures Fundamentals

Files in this Module

Each source file has a companion markdown file with detailed explanations:

File Description
advanced_preprocessor.c 📄 Advanced Preprocessor demonstration
conditional_compilation.c 📄 Conditional Compilation demonstration
preprocessor_demo.c 📄 Preprocessor Demo demonstration

Legend

  • 📄 = Detailed explanation available
  • 🐛 = Contains deliberate bugs for learning