Module 12 of 13 · C Programming Fundamentals · Beginner

Preprocessor & Build

Duration: 45 min

The C preprocessor processes your code before compilation. It handles macros, conditional compilation, and file inclusion. Understanding the preprocessor and build process is essential for writing maintainable C programs.

#include Directive

// Include standard library headers
#include <stdio.h>      // System header (angle brackets)
#include <stdlib.h>
#include <string.h>

// Include custom headers
#include "myheader.h"   // Local header (quotes)

int main() {
    printf("Hello\n");
    return 0;
}

#define Macro

#include <stdio.h>

// Simple macro
#define PI 3.14159
#define MAX_SIZE 100

// Macro with parameters
#define ADD(a, b) ((a) + (b))
#define SQUARE(x) ((x) * (x))

int main() {
    float area = PI * SQUARE(5);
    printf("Area: %.2f\n", area);
    
    int sum = ADD(10, 20);
    printf("Sum: %d\n", sum);
    
    return 0;
}

Conditional Compilation

#include <stdio.h>

#define DEBUG 1

int main() {
    #if DEBUG
        printf("Debug mode ON\n");
    #else
        printf("Debug mode OFF\n");
    #endif
    
    #ifdef DEBUG
        printf("DEBUG is defined\n");
    #endif
    
    #ifndef RELEASE
        printf("Not in release mode\n");
    #endif
    
    return 0;
}

#undef Directive

#include <stdio.h>

#define VALUE 10

int main() {
    printf("VALUE: %d\n", VALUE);
    
    #undef VALUE
    
    // VALUE is no longer defined
    // printf("%d\n", VALUE);  // Error
    
    return 0;
}

Predefined Macros

#include <stdio.h>

int main() {
    printf("File: %s\n", __FILE__);      // Current file name
    printf("Line: %d\n", __LINE__);      // Current line number
    printf("Date: %s\n", __DATE__);      // Compilation date
    printf("Time: %s\n", __TIME__);      // Compilation time
    printf("Function: %s\n", __func__);  // Current function name
    
    return 0;
}

Header Files

// mymath.h
#ifndef MYMATH_H
#define MYMATH_H

int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);

#endif
// mymath.c
#include "mymath.h"

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}
// main.c
#include <stdio.h>
#include "mymath.h"

int main() {
    printf("Add: %d\n", add(5, 3));
    printf("Subtract: %d\n", subtract(5, 3));
    printf("Multiply: %d\n", multiply(5, 3));
    
    return 0;
}

Makefile Example

# Makefile
CC = gcc
CFLAGS = -Wall -g
TARGET = program

SOURCES = main.c mymath.c
OBJECTS = $(SOURCES:.c=.o)

$(TARGET): $(OBJECTS)
	$(CC) $(CFLAGS) -o $(TARGET) $(OBJECTS)

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

clean:
	rm -f $(OBJECTS) $(TARGET)

run: $(TARGET)
	./$(TARGET)

.PHONY: clean run

Compilation Process

// Step 1: Preprocessing
// gcc -E main.c > main.i

// Step 2: Compilation
// gcc -S main.i

// Step 3: Assembly
// gcc -c main.s

// Step 4: Linking
// gcc main.o -o program

// Or all in one:
// gcc -Wall -g main.c -o program

Common Compiler Flags

// -Wall: Enable all warnings
// -g: Include debug symbols
// -O2: Optimize for speed
// -std=c99: Use C99 standard
// -D: Define a macro
// -I: Add include directory
// -L: Add library directory
// -l: Link a library

// Example:
// gcc -Wall -g -O2 -std=c99 main.c -o program

Practical Example: Configuration Header

// config.h
#ifndef CONFIG_H
#define CONFIG_H

#define APP_NAME "MyApp"
#define APP_VERSION "1.0"
#define DEBUG 1

#if DEBUG
    #define LOG(msg) printf("[DEBUG] %s\n", msg)
#else
    #define LOG(msg)
#endif

#endif
// main.c
#include <stdio.h>
#include "config.h"

int main() {
    printf("%s v%s\n", APP_NAME, APP_VERSION);
    LOG("Application started");
    
    return 0;
}

Quiz 1: #include

❓ What is the difference between #include and #include "myheader.h"?

Quiz 2: #define Macro

❓ What does #define do?

Quiz 3: Conditional Compilation

❓ What does #ifdef do?

Quiz 4: Header Guards

❓ What is the purpose of header guards (#ifndef)?

Quiz 5: Compiler Flags

❓ What does the -Wall flag do?

← Previous Continue interactively → Next →

Related Courses