Constants
Prerequisites: Operators
C has two different ways to define a constant, and they behave
quite differently. const creates a real, typed variable the
compiler prevents you from reassigning:
const int MAX_USERS = 100;
#define is a preprocessor macro — pure text substitution,
happening before the compiler even sees types at all:
#define MAX_USERS 100
Prefer const for most cases — it's type-checked and visible to a
debugger; reach for #define mainly for values needed across
multiple files, or true compile-time constants.
Common mistakes
- Adding a semicolon to a
#define:#define MAX_USERS 100;— the semicolon becomes part of the substitution, which can produce confusing errors whereverMAX_USERSis then used.