Skip to main content

#define Statement in C

The #define directive is a preprocessor command that tells the C preprocessor to replace a name with a value (or code) everywhere it appears in the program — before compilation begins. It is the most basic form of a macro in C.

Syntax

  • MACRO_NAME — by convention written in UPPERCASE
  • replacement_text — the value or code that replaces the name
  • No semicolon (;) at the end

Symbolic Constants

The most common use: replacing magic numbers with readable names.
Output:
The preprocessor replaces every occurrence of PI with 3.14159 before the compiler sees the code.

Object-like Macros


Undefining a Macro

You can remove a macro definition mid-file using #undef:

#define vs const

Prefer const for typed constants. Use #define for compile-time substitutions like include guards.