>RunTimeFix
BookStudyPracticeInterviewsYouTubeCoursesAbout
Log inGet Started

Table of Contents

01C++ Foundations & Input/Output02Loops & Direct Simulations03Arrays, Strings & Character Operations04Functions & Recursion05Strings & Character Operations06Structs, Lambdas & Structured Bindings07Preprocessor, Debug Macros & Compilation08Vectors, Pairs & Utility Algorithms09Sets, Maps & Multi-Containers10Sorting & Frequency Arrays11Complexity & Problem Solving12Basic Number Theory & Modular Arithmetic13Prefix Sums & Difference Arrays14Two Pointers & Sliding Window15Binary Search (Array & Answer Space)16Greedy Algorithms & Constructive Problems17Bit Manipulation & Masking18Stacks, Queues & Monotonicity19Disjoint Set Union (DSU)20Coordinate Compression, Fenwick Tree & Segment Tree21Graph Representation, DFS & BFS22Cycle Detection, Bipartite Checking & Topological Sort23Dijkstra, 0-1 BFS & Kruskal's MST24Rooted Trees — DFS, Subtree Size & Diameter25Binary Lifting, LCA & Euler Tour26Dynamic Programming — Foundations27Dynamic Programming — Knapsack & Subsets28Backtracking & Meet-in-the-Middle29Modular Inverse, Combinatorics & Fast Math30Hashing, String Algorithms & Geometry Basics
Study Library/Ch 7
Chapter 07 of 30

Preprocessor, Debug Macros & Compilation

Understand the C++ preprocessor, essential debug macros, and compilation flags for competitive programming.

The C++ Preprocessor & MacrosCompilation Pipeline & G++ Compiler Flags
Share:

Preprocessor, Debug Macros & Compilation

The preprocessor runs before compilation, transforming your source code. Understanding it helps you write shorter contest code and debug faster.

The C++ Preprocessor & Macros

What is a Macro?

A macro is a text substitution that happens before compilation:

hljs cpp
#define ll long long
#define vi vector<int>
#define pii pair<int,int>

ll n;       // becomes: long long n;
vi a;       // becomes: vector<int> a;
pii p;      // becomes: pair<int,int> p;

Macros with Parameters

hljs cpp
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

sort(all(a));       // sort(a.begin(), a.end())
int n = sz(a);      // int n = (int)(a).size();

Macros vs Inline Functions

FeatureMacroInline Function
Type safetyNoYes
DebuggableHarderYes
SpeedSame (text substitution)Same
Debug macrosYesNo

For competitive programming, macros are fine for type aliases and shorthand. For anything with logic, prefer inline functions.

Preprocessor Directives

hljs cpp
#include <bits/stdc++.h>  // include entire standard library
#define MOD 1000000007     // text replacement
#undef MOD                 // undo a #define
#ifdef DEBUG               // conditional compilation
    cerr << "debug: " << x << "\n";
#endif
#ifndef ONLINE_JUDGE       // if not on a judge
    freopen("input.txt", "r", stdin);
#endif

Include Guards

Header files use include guards (or #pragma once) to prevent double inclusion:

hljs cpp
#pragma once
// header content here

Compilation Pipeline & G++ Compiler Flags

Compilation Steps

  1. Preprocessing: #include, #define, #ifdef are resolved
  2. Compilation: C++ code → assembly
  3. Assembly: assembly → object file (.o)
  4. Linking: object files → executable

Essential Flags

hljs bash
g++ -O2 -std=c++17 -Wall -Wextra -o solution solution.cpp
FlagPurpose
-O2Optimization level 2 (always use in contests)
-std=c++17C++17 standard
-WallEnable all common warnings
-WextraEnable additional warnings
-o nameOutput executable name
-gInclude debug symbols (for gdb)
-fsanitize=addressMemory error detector (for debugging)
-DLOCALDefine a macro (for debug builds)

Debug Build

hljs bash
# Debug: no optimization, debug symbols, sanitizers
g++ -O0 -std=c++17 -g -fsanitize=address,undefined -DLOCAL -o debug solution.cpp

# Contest: optimized
g++ -O2 -std=c++17 -o solution solution.cpp

Fast I/O and O2

Always compile with -O2 in contests. Judges use it, so your solution's time complexity assumes optimized code. Without -O2, even correct solutions may TLE.

Summary

  • Macros are text substitution — use for type aliases (#define ll long long) and shorthand
  • #define has no type safety; prefer using for type aliases when possible
  • Always compile with -O2 in contests
  • Use -fsanitize=address,undefined during development to catch bugs
  • #ifndef ONLINE_JUDGE lets you use freopen for local testing without affecting judge submissions
Ch 6: Structs, Lambdas & Structured BindingsCh 8: Vectors, Pairs & Utility Algorithms

Prefer reading offline? Download the full book as a PDF.

Download Free Book
>RunTimeFix

Practical programming education. Books, videos, courses, and hands-on learning for competitive programming, DSA, and software engineering.

RunTimeFix

  • About
  • YouTube
  • Book
  • Study Library
  • Courses

Learn

  • Competitive Programming
  • DSA
  • Development
  • Databases

Legal

  • Privacy
  • Terms
  • Refund Policy

RunTimeFix — debug → solve → run

© 2026 RunTimeFix. All rights reserved.