Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 4 of 10
Constants - const, #define & enum
C has three ways to create named constants: `#define` (preprocessor text substitution - no type, no scope, no address), `const` (typed, scoped, debuggable in gdb, cannot be modified), and `enum` (integer constants with logical grouping). Knowing when to use each is a mark of experienced C code.
Three Kinds of Constants - Compared
#include <stdio.h>
/* ── #define - preprocessor: textual substitution ──────────
- No type. No scope. No address. No debug symbol.
- Before the compiler sees the code, 'MAX_SIZE' is
replaced with the literal text '100'.
- Leads to subtle bugs when macros are expressions. */
#define MAX_SIZE 100
#define PI 3.14159265358979
#define SQUARE(x) ((x) * (x)) /* function-like macro */
/* ── const - typed, scoped, debuggable ─────────────────────
- Has a type: 'const int'. The compiler checks it.
- Has a scope: visible only where declared.
- Has an address: you can pass &max_items to a function.
- Appears in gdb: (gdb) print max_items → 100 */
const int max_items = 100;
const float gravity = 9.81f;
/* ── enum - named integer constants ─────────────────────────
- Values start at 0 unless specified.
- Type-safe alternative to #define for related integers.
- Good for state machines, status codes, option flags. */
typedef enum {
STATUS_OK = 0,
STATUS_ERR = 1,
STATUS_TIMEOUT = 2,
STATUS_RETRY = 3
} Status;
int main(void) {
/* #define */
int arr[MAX_SIZE]; /* MAX_SIZE replaced with 100 */
(void)arr; /* suppress unused-variable warning */
printf("Square of 5: %d
", SQUARE(5));
/* WARNING: SQUARE(1+2) = ((1+2)*(1+2)) = 9 - CORRECT
Without parens: 1+2*1+2 = 5 - WRONG! Always use ()! */
/* const */
printf("Gravity: %.2f m/s^2
", gravity);
/* max_items = 200; ← compile error: assignment to const */
/* enum */
Status s = STATUS_OK;
if (s == STATUS_OK) {
printf("Operation succeeded
");
}
return 0;
}Common Mistakes
- Missing parentheses in function-like macros - `#define DOUBLE(x) x*2` → `DOUBLE(1+3)` = `1+3*2` = 7, not 8. Always wrap both the argument and the whole expression: `#define DOUBLE(x) ((x)*2)`.
- Using `#define` for values that need type checking - `#define BUF_SIZE -1` then `size_t n = BUF_SIZE` silently wraps to `SIZE_MAX`. Use `const int buf_size = -1` so the compiler type-checks assignments.
- Expecting `const` local variables to be compile-time constants for array sizes in C89 - in C89, `const int n = 10; int arr[n];` is illegal (array size must be a compile-time constant). In C99 this works (VLAs), but for fixed sizes, use `#define` or `enum`.
Tip
Tip
Practice Constants const define enum in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Choose between const variables for type safety, #define for preprocessor values, and enum for state sets
Practice Task
Note
Practice Task - (1) Write a working example of Constants const define enum from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Constants const define enum is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- C has three ways to create named constants: `#define` (preprocessor text substitution - no type, no scope, no address), `const` (typed, scoped, debuggable in gdb, cannot be modified), and `enum` (integer constants with logical grouping).
- Missing parentheses in function-like macros - `#define DOUBLE(x) x*2` → `DOUBLE(1+3)` = `1+3*2` = 7, not 8. Always wrap both the argument and the whole expression: `#define DOUBLE(x) ((x)*2)`.
- Using `#define` for values that need type checking - `#define BUF_SIZE -1` then `size_t n = BUF_SIZE` silently wraps to `SIZE_MAX`. Use `const int buf_size = -1` so the compiler type-checks assignments.
- Expecting `const` local variables to be compile-time constants for array sizes in C89 - in C89, `const int n = 10; int arr[n];` is illegal (array size must be a compile-time constant). In C99 this works (VLAs), but for fixed sizes, use `#define` or `enum`.
Finished this lesson?