C course menu
Constants in C: const, #define, enum and Literals
Learn the ways to create constants in C: const variables, #define macros, enum constants, the differences between them, integer, floating, character and string literals with suffixes, and why to avoid magic numbers.
On this page
A constant is a value that does not change while the program runs, such as a tax rate, a maximum size or the number of days in a week. Naming constants makes code easier to read and change.
const variables#
const makes a variable read-only after it is initialised:
#include <stdio.h>
int main(void) {
const double GST_RATE = 0.18;
const int DAYS_IN_WEEK = 7;
printf("%.2f\n", 1000 * GST_RATE);
printf("%d hours\n", DAYS_IN_WEEK * 24);
return 0;
}180.00
168 hoursTrying to change a const is a compile error:
#include <stdio.h>
int main(void) {
const int MAX_USERS = 100;
MAX_USERS = 200;
printf("%d\n", MAX_USERS);
return 0;
}GCC reports error: assignment of read-only variable 'MAX_USERS'.
#define macros#
The preprocessor replaces every use of a #define name with its text before compiling:
#include <stdio.h>
#define PI 3.14159
#define MAX_NAME 50
int main(void) {
double r = 2.0;
char name[MAX_NAME];
printf("Area: %.2f\n", PI * r * r);
printf("Buffer size: %zu\n", sizeof name);
return 0;
}Area: 12.57
Buffer size: 50- No
=and no semicolon:#define PI 3.14159. - By convention, macro names are UPPER_CASE.
- Macros are useful for array sizes and compile-time settings, as above.
const or #define?#
Prefer const for typed values in modern C, and use #define (or enum) where you need a true compile-time constant, such as the size of a global array. Macros are covered in The Preprocessor and Macros.
enum constants#
An enum defines a group of named integer constants:
#include <stdio.h>
enum Weekday { MON, TUE, WED, THU, FRI, SAT, SUN };
int main(void) {
enum Weekday today = WED;
printf("%d\n", today);
printf("%d\n", SUN - MON + 1);
return 0;
}2
7Values start at 0 unless you set them. Enums are covered in Unions and Enums.
Literals#
Values written directly in code are literals:
printf("%d %d %d\n", 0x1F, 017, 31);
printf("%.0f\n", 1e6);31 15 31
1000000Note the octal trap: 017 is 15, not 17. Never write leading zeros on decimal numbers.
Avoid magic numbers#
if (age >= 18 && marks > 39) { ... } /* what do 18 and 39 mean? */
#define VOTING_AGE 18
const int PASS_MARK = 40;
if (age >= VOTING_AGE && marks >= PASS_MARK) { ... }Named constants explain themselves and need changing in only one place.
Practice#
Practice exercise
Define the speed of light as a macro (299792 km/s) and the number of seconds in a minute as a const int. Print how many kilometres light travels in one minute.
Show solution
#include <stdio.h>
#define LIGHT_KM_PER_S 299792
int main(void) {
const int SECONDS_PER_MINUTE = 60;
long long km = (long long)LIGHT_KM_PER_S * SECONDS_PER_MINUTE;
printf("%lld km\n", km);
return 0;
}17987520 kmThe cast to long long keeps the multiplication safe even for larger values.
Key takeaway#
Finished this lesson?