Conditional Compilation in C
#ifdef/#ifndef/#if/#elif/#else/#endif compile different code per configuration — platforms (#ifdef _WIN32), debug builds (#ifndef NDEBUG), feature flags, and header capability probes. Combined with -D command-line defines (gcc -DDEBUG=1), one source tree serves many targets.
Reserve it for genuine variability (platforms, optional features); #ifdef mazes around trivial differences rot fast. Keep conditional blocks small, symmetric, and tested in every configuration CI builds.
Platform branches
#ifdef _WIN32 ... #else ... #endif compiles Windows vs POSIX paths — Sleep vs sleep, backslashes vs slashes.
Debug scaffolding
#ifndef NDEBUG debug-only checks and logging compile out of release builds entirely — zero overhead shipped.
-D defines from builds
gcc -DVERSION=\"1.2\" injects configuration without editing sources — CI stamps builds this way.
Keep blocks tiny
Conditionalize small platform seams, not whole architectures — sprawling #ifdef forests become untestable.
#include <stdio.h> int main(void) {#ifdef _WIN32 printf("windows\n");#else printf("posix\n");#endif return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.