Preprocessor & Build System

Master the C preprocessor. Understand macros, includes, header guards, and how to structure professional C projects.

#define: Text Substitution

The preprocessor replaces text before compilation. No type checking.

macros.c
#define PI 3.14159 #define MAX_SIZE 1000 int main() { float area = PI * 5 * 5; int arr[MAX_SIZE]; }

Function-like Macros

Faster than functions (no overhead), but dangerous.

#define SQUARE(x) ((x) * (x)) int y = SQUARE(5); // 25

The Danger

Side effects are duplicated.

SQUARE(i++); // Expands to: ((i++) * (i++)) // i increments TWICE! Undefined.

#include: File Insertion

Literally copies the contents of one file into another.

System Headers

Uses angle brackets <>. Searches system paths.

#include <stdio.h> #include <stdlib.h>

User Headers

Uses quotes "". Searches current directory first.

#include "myheader.h" #include "config.h"

Header Guards

Preventing duplicate definitions when files are included multiple times.

myheader.h
#ifndef MYHEADER_H #define MYHEADER_H struct Point { int x, y; }; void move(struct Point *p); #endif
Modern Alternative: #pragma once is supported by most modern compilers and does the same thing more cleanly, though it is non-standard.

Conditional Compilation

Compiling different code blocks based on conditions.

#ifdef _WIN32 #include <windows.h> #else #include <unistd.h> #endif #if DEBUG_LEVEL > 0 printf("Debug Mode On\n"); #endif