Function-like Macros

Macros created by the #define directive are named snippets of code. For constants, the name reflects a literal value. On a more complex level, you can also assign actions (executed statements or logical chunks) to a macro, creating a function-like macro:

#define ALERT printf("Danger!")

With this directive, uses of ALERT in your C code will be replaced with that printf() statement.

As always, there is a catch here: C preprocessor directives must be defined on a single line. Once a new line is encountered, the old directive's definition is over. When the information for a directive won't fit on a single line (which is most likely to happen with function-like macros), you can break up the line by inserting a space followed ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.