liveBook · Manning

Macro

As explained in Modern C

A define directive is a C preprocessor directive that defines a macro and its replacement text.

# define M_PI 3.14159265358979323846


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":8}]]"}
!@%STYLE%@!

This definition replaces later occurrences of M_PI with the double constant 3.14159265358979323846. section 5.6.3

A macro defines a textual replacement for program code. A #define directive has a leading #, the keyword define, an identifier, replacement text, and a terminating newline. The # must be the first non-blank character on its line. section 5.6.3

Macros can name constants of types such as unsigned, size_t, and double. Replacement alone does not make a macro a compile-time constant: the replacement must itself have the appropriate constant form. TAKEAWAY: Use all-capital macro names unless there is a good reason not to; standard macros do not always follow this convention. section 5.6.3 section 5.6

#define BYTECOPY(T, S) memcpy(&(T), &(S), MINSIZE(T, S))


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":7}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":29}]]"}
!@%STYLE%@!

BYTECOPY is a function-like macro: its parameters T and S stand for argument text in the replacement. MINSIZE is another macro used within that replacement. section 16.1

For a call such as BYTECOPY(A, B), arguments are separated by commas outside nested parentheses and expanded for macros. Their resulting text replaces the parameters; the result is scanned again for macro replacement. During this process, the macro being expanded is temporarily disabled to prevent infinite recursion. section 16.1

Textual substitution is not the same as a function call. A macro that uses a parameter twice may evaluate an argument twice. Parenthesize arguments in replacement text, avoid repeated evaluation, and avoid hidden effects. A function is the primary choice for modularization; a function-like macro should provide a simple interface to a complex task. section 16.1 section 16.0 section 10.2

A dangling else

With #define ERRORCHECK(CODE) if (CODE) return -1, writing if (a) ERRORCHECK(x); else puts("a is 0!"); expands to if (a) if (x) return -1; else puts("a is 0!");. The else attaches to the inner if, not the visually apparent outer one. The macro also hides a return that its name does not suggest. section 10.2.1

1   #define ERROR_RETURN(CODE) \
2   do {                       \
3     if (CODE) return -1;     \
4   } while (false)


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":2,\"ch\":24},{\"line\":2,\"ch\":25}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":11}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":6}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":2,\"ch\":6},{\"line\":2,\"ch\":8}],[{\"line\":2,\"ch\":16},{\"line\":2,\"ch\":22}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":3,\"ch\":6},{\"line\":3,\"ch\":11}],[{\"line\":3,\"ch\":13},{\"line\":3,\"ch\":18}]]"}
!@%STYLE%@!

ERROR_RETURN makes the possible return explicit in its name. Its do-while(false) wrapper lets the replacement behave as a single statement, avoiding the dangling-else problem at the call site; use this technique sparingly. section 10.2.1

Macros also appear in preprocessor conditions for platform checks. Use macros and integer literals in these conditions; an unknown identifier evaluates to 0. An #error directive inside a conditional can stop compilation when a required platform feature is absent. section 8.1.5

How Modern C covers it

How deep it goes
Explains directive syntax and function-like expansion, then analyzes repeated evaluation and control-flow hazards.
Examples
Uses M_PI, BYTECOPY, a repeated-evaluation square macro, and a dangling-else error check.
What you can do after
Build simple macros from scratch while inspecting expansion for repeated evaluation and surprising control flow.
How it connects
Connects macros to named constants, preprocessor conditionals, functions, and type-generic interfaces.

Also explained in

Compare how 10 books explain macro →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.