On this page
  1. Preprocessor & Macros
  2. Contents
  3. Header Guards & Safe Macro Hygiene
  4. Prefer static inline & enum Over Macros

Preprocessor & Macros

Prev: Build Pipeline, Linking & ABI | Up: compilation_and_internals | Next: Memory Layout, Alignment & Qualifiers
Rel: Build Pipeline, Linking & ABI
Ref: cppreference — Preprocessor
Tags: #public #prog #c #internals #macros


Contents


Header Guards & Safe Macro Hygiene

Always wrap headers with include guards to prevent duplicate definitions:

#ifndef MY_MODULE_H
#define MY_MODULE_H

int compute(int x);

#endif /* MY_MODULE_H */

For multi-statement macros, wrap in do { ... } while (0) so they behave as a single statement inside if (...) MACRO(); else ..., and parenthesize every parameter (((a) < (b) ? (a) : (b))). Beware double evaluation: MIN(i++, j++) increments twice!


Prefer static inline & enum Over Macros

Linked from

Related notes