#define Define object-like and function-like macros
#define Define object-like and function-like macros
#define creates object-like or function-like macros expanded before compilation, not function calls. Parenthesize results; arguments may be reevaluated.
| Category | Preprocessor directives |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
#define NAME replacement-text
#define NAME(parameter-list) replacement-text
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
area = 12.57
square = 16
⚠️ Warning Parenthesize every parameter and the entire replacement in a function-like macro, as in ((x) * (x)). A macro may evaluate an argument more than once, so avoid side-effecting expressions such as SQUARE(i++).
🏷️ Related preprocessor directives
📚 Related Tutorials
❓ FAQ
QWhat is the difference between object-like and function-like macros?
AAn object-like macro has only replacement text after its name. A function-like macro has a parameter list immediately after the name in its definition, as in #define SQUARE(x) ((x) * (x)); no space may separate the name from the opening parenthesis.
QWhat do the # and ## operators do in C macros?
AWithin macro replacement lists, # converts a function-like macro argument into a string literal, and ## pastes preprocessing tokens together. They are macro operators, not standalone preprocessing directives.
QWhy should macro parameters and the entire expression be parenthesized in C?
AMacro expansion is textual substitution, so arguments are inserted into the surrounding expression. Without parentheses around parameters and the full replacement, operators inside or outside the macro can change the intended grouping. Parentheses preserve grouping but cannot prevent an argument from being evaluated more than once.