On this page
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
- Use
enum { BUF_SIZE = 256 };for compile-time integer constants (visible to debuggers and scoped). - Use
static inlinefunctions instead of function-like macros whenever possible for type safety and single argument evaluation.
Linked from
- tuts/c/builtins/__STDC_VERSION__
- tuts/c/builtins/_Generic
- tuts/c/builtins/inline
- tuts/c/builtins/typeof
- tuts/c/compilation_and_internals
- tuts/c/compilation_and_internals/build_pipeline_and_linking
- tuts/c/compilation_and_internals/memory_layout_and_abi
- tuts/c/compilation_and_internals/preprocessor_and_macros
Related notes
- tuts/c/compilation_and_internals Linked here; links to this page; shared tags: #c, #internals, #prog
- tuts/c/compilation_and_internals/build_pipeline_and_linking Linked here; links to this page; shared tags: #c, #internals, #prog
- tuts/c/compilation_and_internals/memory_layout_and_abi Linked here; links to this page; shared tags: #c, #internals, #prog
- tuts/c/builtins/__STDC_VERSION__ links to this page; shared tags: #c, #prog
- tuts/c/builtins/_Generic links to this page; shared tags: #c, #prog
- tuts/c/builtins/inline links to this page; shared tags: #c, #prog
- tuts/c/builtins/typeof links to this page; shared tags: #c, #prog
- tuts/c/compilation_and_internals/error_handling_and_cleanup shared tags: #c, #internals, #prog
- tuts/c shared tags: #c, #prog
- tuts/c/builtins shared tags: #c, #prog