Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Preprocessor

The #define directive

Reference for the C #define directive: object-like and function-like macros, rescanning, redefinition rules, multi-line macros and typical mistakes.

#define creates a macro: a name that the preprocessor replaces with other tokens wherever it appears in the rest of the file. There are two kinds. An object-like macro is just a name and a replacement (#define PI 3.14159). A function-like macro has a parameter list directly after the name and substitutes its arguments into the replacement (#define SQUARE(x) ((x) * (x))).

Macros work on tokens, not on values or types. The replacement is rescanned for more macro names, and a macro is never expanded inside its own expansion, which prevents infinite recursion.

C
#define BUFFER_SIZE 256
#define SQUARE(x) ((x) * (x))

Try it

C Object-like and function-like macros

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Multi-line macros and rescanning

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#define identifier replacement-list
#define identifier(parameters) replacement-list
#define identifier(parameters, ...) replacement-list    /* variadic */
Name Description
identifier The macro name. Convention: upper case. Must not be a keyword used as a name in a hosted program, and names starting with __ or _ + capital are reserved.
parameters Comma-separated names; the opening parenthesis must follow the name with no space.
replacement-list Tokens that replace the macro. A trailing backslash continues the line.

Errors: redefining a macro with a different replacement is a constraint violation (compilers warn or error); an identical redefinition is allowed. Wrong argument count in a call is an error.

Usage notes

  • No space, big difference. #define F (x) is an object-like macro that expands to (x). #define F(x) is function-like.
  • Parenthesize every parameter use and the whole replacement, or operator precedence will change the meaning. See Macro pitfalls.
  • No trailing semicolon in the definition, or x = LIMIT; becomes x = (...);;.
  • Arguments may be evaluated more than once, so avoid side effects in them.
  • Scope is textual: a macro lives from its #define to the end of the file or to #undef. It ignores functions, blocks and namespaces.
  • A function-like macro name not followed by ( is left alone, so a function and a macro may share a name (the parenthesized call (f)(x) bypasses the macro).
  • Prefer alternatives for constants: an enum or a C23 constexpr object has a type and a scope. Keep macros for conditional compilation, stringification and token pasting.
  • Predefine from the command line with -DNAME=value.