#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 UPPERCASEreplacement_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.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
#define | const | |
|---|---|---|
| Handled by | Preprocessor | Compiler |
| Type checking | None | Yes |
| Memory | None allocated | Allocated |
| Scope | File-wide only | Follows block rules |
| Debugging | Harder | Easier |
const for typed constants. Use #define for compile-time substitutions like include guards.