C Constants
Some values should never change while a program runs: the number of days in a week, the speed of light, the size of a buffer. Marking such values as constants documents your intent and lets the compiler catch accidental changes. C offers three ways to create them: the const keyword, #define macros and enum constants.
The const keyword
Put const in front of a declaration and the variable becomes read-only. It must be given its value when it is declared:
#include <stdio.h>
int main(void) {
const int DAYS_PER_WEEK = 7;
const double VAT_RATE = 0.21;
int weeks = 4;
double net = 50.0;
printf("%d weeks = %d days\n", weeks, weeks * DAYS_PER_WEEK);
printf("Price with VAT: %.2f\n", net * (1 + VAT_RATE));
return 0;
}4 weeks = 28 days
Price with VAT: 60.50
Trying to change a const variable is a compile error:
const int MAX_PLAYERS = 4;
MAX_PLAYERS = 5; // error: assignment of read-only variable 'MAX_PLAYERS'And declaring it without a value is pointless, because you can never assign it later:
const int LIMIT; // LIMIT is stuck with garbage (or 0 if global) forever
LIMIT = 10; // errorTip: Writing constant names in UPPER_CASE is a widespread convention that makes them stand out from ordinary variables.
#define macros
Before const existed, C programmers used the preprocessor to define constants. #define NAME value tells the preprocessor to replace every later occurrence of NAME with value before the compiler even sees the code:
#include <stdio.h>
#define MAX_SCORE 100
#define PI 3.14159
#define GREETING "Welcome!"
int main(void) {
int score = 87;
printf("%s\n", GREETING);
printf("You scored %d out of %d\n", score, MAX_SCORE);
printf("Circle area (r=2): %.3f\n", PI * 2 * 2);
return 0;
}Welcome!
You scored 87 out of 100
Circle area (r=2): 12.566
Note that there is no = and no semicolon in a #define. Whatever follows the name is pasted in literally, so #define MAX 100; would put a stray semicolon into every use.
const vs. #define
const variable |
#define macro |
|
|---|---|---|
| Has a type | Yes, checked by the compiler | No, it is plain text substitution |
| Respects scope (block/function) | Yes | No, valid from the definition to the end of the file |
| Visible in a debugger | Yes | Usually not |
Usable as an array size at file scope / in case labels |
No (in C) | Yes |
Modern C style prefers const for typed values and #define (or enum) where a true compile-time constant is required, such as array sizes in global declarations.
enum constants
An enum defines a group of named integer constants. They are true compile-time constants with a scope, which makes them a good alternative to #define for related integers:
#include <stdio.h>
enum { ROWS = 3, COLS = 4 };
int main(void) {
int grid[ROWS][COLS] = {0};
printf("The grid has %d cells\n", ROWS * COLS);
printf("sizeof(grid) = %zu bytes\n", sizeof(grid));
return 0;
}The grid has 12 cells
sizeof(grid) = 48 bytes
Enums are covered fully in C Enums.
Literal constants
Values written directly in code — 42, 3.5, 'x', "text" — are called literals. Suffixes and prefixes change their type or base:
#include <stdio.h>
int main(void) {
printf("%d %d %d\n", 0x1F, 017, 0b101); // hex, octal, binary (GCC extension, standard in C23)
printf("%ld %lu\n", 100000L, 4000000000UL);
printf("%f\n", 2.5f);
printf("%e\n", 6.02e23);
return 0;
}31 15 5
100000 4000000000
2.500000
6.020000e+23
| Literal | Meaning |
|---|---|
0x1F |
hexadecimal (31) |
017 |
octal (15) — a leading zero changes the base! |
10L, 10UL, 10LL |
long, unsigned long, long long |
2.5f |
float instead of double |
6.02e23 |
scientific notation: 6.02 × 10²³ |
Watch out:
int code = 010;stores 8, not 10, because a leading zero means octal.
Test yourself
What happens when you assign a new value to a const int variable?
- It changes normally
- The compiler reports an error
- It is silently ignored at runtime
- The program crashes
Which #define is written correctly?
- #define SIZE = 10;
- #define SIZE 10
- #define SIZE 10;
- define SIZE 10
What value does the literal 012 have?
- 12
- 10
- 2
- It is invalid