Skip to content
Laviyx Learn

Try

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.

Beginner6 min readUpdated
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:

C
#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;
}
Output
180.00
168 hours

Trying to change a const is a compile error:

C
#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:

C
#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;
}
Output
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?#

const#define
Has a typeYes, checked by the compilerNo, plain text substitution
ScopeFollows normal block scopeFrom the definition to the end of the file
Visible in a debuggerUsuallyNo
Usable as an array size everywhereNot always in CYes

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:

C
#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;
}
Output
2
7

Values start at 0 unless you set them. Enums are covered in Unions and Enums.

Literals#

Values written directly in code are literals:

LiteralType
42, -7int
42u, 42L, 42LL, 42ULLunsigned, long, long long, unsigned long long
0x1F, 017, 0b1010Hexadecimal, octal (leading 0), binary (C23 and GCC extension)
3.14, 1e6double
3.14ffloat
'A', '\n'Character (an int value in C)
"text"String: an array of char ending with '\0'
C
printf("%d %d %d\n", 0x1F, 017, 31);
printf("%.0f\n", 1e6);
Output
31 15 31
1000000

Note the octal trap: 017 is 15, not 17. Never write leading zeros on decimal numbers.

Avoid magic numbers#

C
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
C
#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;
}
Output
17987520 km

The cast to long long keeps the multiplication safe even for larger values.

Key takeaway#

Finished this lesson?