Constants – #define, const, enum
What is a Constant?
A constant is a value that cannot be changed during program execution. Unlike variables, once a constant is defined, its value remains fixed throughout the program. Constants make code:
- More readable —
PIis clearer than3.14159 - Easier to maintain — change the value in one place only
- Safer — compiler error if you try to modify a constant
C provides three ways to define constants: #define, const, and enum.
#define – Preprocessor Macro
The #define directive creates a symbolic constant (also called a macro). The preprocessor replaces every occurrence of the macro with its value before compilation — it's a simple text substitution.
#define CONSTANT_NAME value // Note: No semicolon! No data type! No = sign!
#include <stdio.h> #define PI 3.14159 #define MAX_SIZE 100 #define SCHOOL "Alpine Public School" #define TRUE 1 #define FALSE 0 #define SQUARE(x) ((x) * (x)) // Macro with parameter int main() { float radius = 7.0f; float area = PI * radius * radius; printf("Area of circle = %.2f\n", area); printf("School: %s\n", SCHOOL); printf("Square of 5 = %d\n", SQUARE(5)); printf("Max array size = %d\n", MAX_SIZE); return 0; }
By convention, constants defined with #define are written in ALL_CAPS with underscores. This makes them easily distinguishable from variables.
Always use parentheses in macro expressions: #define SQUARE(x) ((x)*(x)) not #define SQUARE(x) x*x. Without parentheses, SQUARE(2+3) becomes 2+3*2+3 = 11 instead of 25!
const Keyword
The const keyword makes a variable read-only. Unlike #define, it has a data type and follows all variable scoping rules.
#include <stdio.h> int main() { const float PI = 3.14159f; const int MAX = 100; const char GRADE = 'A'; printf("PI = %.5f\n", PI); printf("MAX = %d\n", MAX); printf("GRADE = %c\n", GRADE); // PI = 3.14; // ERROR! Cannot modify const // MAX = 200; // ERROR! return 0; }
enum – Named Integer Constants
enum (enumeration) lets you create a set of related named integer constants. It improves code readability by replacing magic numbers with meaningful names.
#include <stdio.h> // By default: MON=0, TUE=1, WED=2, THU=3, FRI=4, SAT=5, SUN=6 enum Day { MON, TUE, WED, THU, FRI, SAT, SUN }; // Custom values enum Status { PENDING = 1, ACTIVE = 2, INACTIVE = 3, DELETED = 99 }; // Traffic light enum Light { RED, YELLOW, GREEN }; int main() { enum Day today = FRI; printf("Friday value = %d\n", today); // 4 printf("Sunday value = %d\n", SUN); // 6 enum Light signal = GREEN; if (signal == GREEN) { printf("Go! Signal is GREEN\n"); } enum Status userStatus = ACTIVE; printf("Status code = %d\n", userStatus); // 2 return 0; }
#define vs const
| Feature | #define | const |
|---|---|---|
| Processing | Preprocessor (before compile) | Compiler (during compile) |
| Data type | No type — just text substitution | Has a specific data type |
| Memory | No memory allocated | Memory allocated (like a variable) |
| Scope | Global (from definition to end of file) | Follows block scope rules |
| Debugging | Harder (no type info) | Easier (type checking by compiler) |
| Pointer | Cannot get address | Can get address |
| Best for | Simple values, macros | Typed constants, local constants |
Summary
- Constants = values that don't change during program execution
#define NAME value— preprocessor text substitution, no type, no semicolonconst type NAME = value;— typed constant, compiler-checkedenum— set of related named integer constants, starts at 0 by default- Use ALL_CAPS naming for constants by convention
- Prefer
constover#definefor type safety (modern C practice)
Frequently Asked Questions
What is a constant in C?
What is the difference between #define and const in C?
#define creates a preprocessor macro that is simply text-substituted before compilation and has no type. const creates a real typed variable that cannot be modified, so the compiler checks its type and it can be inspected while debugging. const is generally preferred for typed constants.How do you declare a constant in C?
#define PI 3.14159 as a preprocessor macro, or declare a variable with the const keyword such as const int DAYS = 7;. Both give you a value that should not change during the program.What is a symbolic constant in C?
#define, so you can write a meaningful name instead of a raw number throughout your code. Changing the value then means editing just one line, which reduces errors.Why use constants instead of literal values?
MAX_USERS explains its meaning, and if the value needs to change you update it in one place. It also prevents accidental changes to values that must stay fixed.