Constants & literals
6 min lesson
- A constant is a value that cannot change while the program runs, unlike a variable which can be reassigned.
- Constants matter because some values should never change, like the number of days in a week or the value of pi, and protecting them prevents accidental bugs.
- Think of a constant as a number printed on a label that is glued down. You can read it many times, but you cannot peel it off and write a new number.
- A literal is a value written directly in the code, like 42, 3.14 or 'A'. It has a type based on how it is written.
- The const keyword marks a variable as read-only: const int daysInWeek = 7;. Trying to change it later is a compile error.
- #define creates a named constant before compilation: #define PI 3.14159. The preprocessor replaces every PI with the number.
- The computer stores const variables just like normal variables in memory, but the compiler refuses to let you assign to them.
- The syntax is: const type name = value; or #define NAME value (with no semicolon).
- A realistic use: defining a tax rate or a maximum login attempts value that should stay fixed throughout the program.
- A surprising detail: #define has no type and no semicolon, and it does plain text replacement, which can cause surprising bugs if you are not careful.
- The example program defines a constant for the number of days in a week and a const double for pi, then uses them to compute and print values.
- Use const for values that should not change; it documents your intent and lets the compiler catch mistakes.
- Prefer const over #define in modern C because const has a type and respects scope.
- Remember that #define lines have no semicolon and no equals sign.
- Name constants in ALL_CAPS like MAX_USERS so they stand out.
- You are now writing safer code. Nice work!
#include <stdio.h>
// A preprocessor constant: every DAYS_IN_WEEK becomes 7 before compiling
#define DAYS_IN_WEEK 7
int main(void) {
// A const variable: the compiler will refuse any attempt to change it
const double pi = 3.14159;
// A normal variable we are allowed to change
int weeks = 3;
// Multiply weeks by the constant to get total days
int totalDays = weeks * DAYS_IN_WEEK;
// Print the result with a clear label
printf("Days in %d weeks: %d\n", weeks, totalDays);
// Use the pi constant to compute a circle's circumference
double radius = 2.0;
double circumference = 2.0 * pi * radius;
// Print the circumference with two decimal places
printf("Circumference: %.2f\n", circumference);
return 0;
}What happens: Days in 3 weeks: 21 Circumference: 12.57
Breaking it down
Preprocessor define
#define DAYS_IN_WEEK 7Tells the preprocessor to replace every DAYS_IN_WEEK with 7 before the compiler sees the code. No semicolon is used. This is a text substitution, not a variable.
const variable
const double pi = 3.14159;Creates a real variable in memory but marks it read-only. The compiler will give an error if you try to assign to it later.
Normal variable
int weeks = 3;A regular variable that can change during the program. It is used here to show how a constant combines with a variable.
Using the constant
weeks * DAYS_IN_WEEKThe compiler sees 3 * 7 after preprocessing. The constant name makes the intent clear to a human reader.
Using pi
2.0 * pi * radiusComputes the circumference of a circle. Using a named constant avoids typing the digits of pi in many places, which prevents typos.
A helpful analogy
A constant is like the printed price on a menu board. Customers can read it as many times as they want, but the staff cannot scribble a new number on it. If the price needs to change, someone must print a new board (recompile).
Nesting rules
- 1.A const variable must be initialized when declared; you cannot set it later.
- 2.#define lines have no semicolon and no equals sign, and they are not scoped to a function.
- 3.Name constants in ALL_CAPS so readers instantly see they should not be changed.
Common mistakes
Putting a semicolon after a #define
Avoid
#define MAX 100; // semicolon breaks itUse instead
#define MAX 100The semicolon becomes part of the replacement text, so int x = MAX; becomes int x = 100;; which is a compile error.
Trying to change a const variable
Avoid
const int x = 5;
x = 10; // errorUse instead
int x = 5;
x = 10; // fineThe compiler refuses to assign to a const variable. If the value really needs to change, it should be a normal variable.
Assuming #define has a type
Avoid
#define PI 3.14
int x = PI; // loses decimalUse instead
const double PI = 3.14;
double x = PI;#define has no type; the compiler sees the literal 3.14 wherever it appears, but the surrounding context decides what happens.
Quick tips
- •Prefer const over #define in modern C because const has a type and scope.
- •Use ALL_CAPS names for constants so they stand out in code.
- •Try changing a const variable and read the compiler error; it teaches you the rule quickly.
- •Group related constants near the top of the file so they are easy to find.
- •Remember that a literal like 42 or 'A' is itself a kind of constant written directly in code.