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 live online training, plus books, videos, and digital content from nearly 200 publishers.