C - Constants
Constants represent values that should not change.
Ways to Define Constants
const
variables (type-safe):const int MAX = 100;
- Preprocessor macros (no type checking):
#define MAX 100
Examples
#include <stdio.h>
#define BUFFER 1024
const double PI = 3.141592653589793;
int main(void) {
printf("%d %.2f\n", BUFFER, PI);
}
Common Pitfalls
- Writing to a
const
variable (compiler error). - Macros lack scope and type safety; prefer
const
when possible.