#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.

CategoryPreprocessor directives
KindKeyword / Statement
C standardC89 / 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 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏