Skip to content

Constants in C

Updated 6 min read Aimerscrew

Some values in a program should never change: the number of days in a week, a sales tax rate, the seats on a plane. Constants in C let you give these values a name and lock them so they can't be changed by accident. In this lesson, you'll learn the three kinds of constants: literals, const variables and #define names.

🌍 Real-life example

A movie theater has exactly 150 seats. That number is painted on the fire safety sign by the door, and nobody gets to change it on a busy night. The number of tickets sold changes all evening, but the maximum stays fixed. In C, tickets sold would be a variable, and the seat limit would be a constant.

What is a constant in C?

A constant is a value that stays the same for the whole run of the program. If code tries to change a const value, the compiler refuses to build the program. That protection is the whole point: constants turn "please don't change this" into a rule the compiler checks for you.

Literals: values written directly in code

The simplest constants are literals, the raw values you type straight into your code:

#include <stdio.h>

int main(void) {
    printf("%d\n", 250);       /* integer literal */
    printf("%.2f\n", 19.99);   /* floating-point literal */
    printf("%c\n", 'Z');       /* character literal */
    printf("%s\n", "Hello");   /* string literal */
    printf("%d\n", 0x1F);      /* hexadecimal integer literal */
    return 0;
}
250
19.99
Z
Hello
31

The 0x prefix means the number is written in hexadecimal (base 16), and 1F in hex is 31. The %s placeholder prints text; you'll see it again in format specifiers.

LiteralKindType
42Integerint
42uUnsigned integerunsigned int
42LLBig integerlong long
3.14Floating-pointdouble
3.14fFloating-pointfloat
'A'Characterint (holds a character code)
"Hi"StringArray of char

The const keyword

Putting const in front of a declaration makes a read-only variable. You must give it a value when you declare it, because you won't be allowed to assign one later.

#include <stdio.h>

int main(void) {
    const double TAX_RATE = 0.08;
    double price = 50.00;
    double tax = price * TAX_RATE;

    printf("Price: %.2f\n", price);
    printf("Tax: %.2f\n", tax);
    printf("Total: %.2f\n", price + tax);
    return 0;
}
Price: 50.00
Tax: 4.00
Total: 54.00

If the tax rate changes next year, you update one line and every calculation follows. Compare that with typing 0.08 in ten different places and hoping you find them all.

Constants also make calculations read like plain English:

#include <stdio.h>

int main(void) {
    const int SECONDS_PER_MINUTE = 60;
    const int MINUTES_PER_HOUR = 60;
    const int HOURS_PER_DAY = 24;

    int seconds_per_day = SECONDS_PER_MINUTE * MINUTES_PER_HOUR * HOURS_PER_DAY;
    printf("A day has %d seconds\n", seconds_per_day);
    return 0;
}
A day has 86400 seconds

#define: named constants from the preprocessor

The other common way to name a constant is #define. It's handled by the preprocessor, the step that runs before the real compiler (you saw it in how C compiles). It works like find-and-replace: every time the name appears, it's swapped for the value.

#include <stdio.h>

#define MAX_SEATS 150

int main(void) {
    int booked = 138;
    int available = MAX_SEATS - booked;

    printf("Seats on this flight: %d\n", MAX_SEATS);
    printf("Booked: %d\n", booked);
    printf("Still available: %d\n", available);
    return 0;
}
Seats on this flight: 150
Booked: 138
Still available: 12

Notice the syntax: #define NAME value with no equals sign and no semicolon. It's a preprocessor instruction, not a C statement. You'll explore #define in depth in C preprocessor macros.

Left: #define MAX_SEATS 150 makes the preprocessor turn available = MAX_SEATS - booked; into available = 150 - booked; before compiling. Right: const double TAX_RATE = 0.08; creates a locked, read-only box in memory
#define swaps text before compiling; const creates a real, typed variable that can't be changed.

Why are constants written in UPPER_CASE?

C doesn't require it, but almost every C programmer writes constant names in UPPER_CASE with underscores: MAX_SEATS, TAX_RATE, HOURS_PER_DAY. When you see a capitalized name, you instantly know it's fixed. Ordinary variables stay in lowercase, like booked.

const vs #define: what's the difference?

const#define
Handled byThe compilerThe preprocessor (before compiling)
Has a type?Yes, e.g. doubleNo, it's plain text
Uses memory?Usually, like a variableNo, the value is pasted in
Syntaxconst int MAX = 10;#define MAX 10
Visible where?Inside its block, like a variableFrom the #define line to the end of the file
Typical useTyped values inside functionsProgram-wide limits and settings

💡 Tip

Both are fine for beginners. A good rule of thumb: use const when you want type checking for a value inside a function, and #define for simple, program-wide numbers like a maximum size. You'll see both in real C code.

Real-world example: a parking lot

A parking garage has a fixed number of spots and a fixed hourly rate. Those are constants. The number of cars and the time parked change all day, so those are variables:

#include <stdio.h>

#define TOTAL_SPOTS 200

int main(void) {
    const double HOURLY_RATE = 2.75;
    int spots_taken = 163;
    int hours_parked = 4;

    printf("Free spots: %d of %d\n", TOTAL_SPOTS - spots_taken, TOTAL_SPOTS);
    printf("Fee for %d hours: $%.2f\n", hours_parked, hours_parked * HOURLY_RATE);
    return 0;
}
Free spots: 37 of 200
Fee for 4 hours: $11.00

Common mistakes with constants

1. Trying to change a const

const double TAX_RATE = 0.08;
TAX_RATE = 0.10;
error: assignment of read-only variable 'TAX_RATE'

That error is the compiler doing its job. If a value really needs to change, it shouldn't be a constant.

2. Declaring a const without a value

const int LIMIT; followed by LIMIT = 10; fails for the same reason: you can't assign to a const after declaring it. Always write const int LIMIT = 10; in one go.

3. Adding = or ; to a #define

#define MAX_SEATS = 150   /* wrong: no = */
#define MAX_SEATS 150;    /* wrong: no ; */

Because #define pastes text exactly, the = or ; gets pasted too. MAX_SEATS - booked would become = 150 - booked (a syntax error) or 150; - booked (which ends the statement early and throws the subtraction away). Write just #define MAX_SEATS 150.

4. Using "magic numbers"

Code like total = price * 1.08; works, but nobody knows what 1.08 means. Give it a name, such as const double TAX_MULTIPLIER = 1.08;, and your code explains itself.

Practice exercise

🏋️ Try it yourself

A gym class has room for 25 people (use #define) and costs 12.50 per person (use const). 18 people have signed up. Print the number of free places and the total money collected, with two decimals.

Show solution
#include <stdio.h>

#define CLASS_SIZE 25

int main(void) {
    const double PRICE_PER_PERSON = 12.50;
    int signed_up = 18;

    printf("Free places: %d\n", CLASS_SIZE - signed_up);
    printf("Money collected: $%.2f\n", signed_up * PRICE_PER_PERSON);
    return 0;
}
Free places: 7
Money collected: $225.00

What's next?

You've been printing values with %d, %.2f and %c. Next, learn every placeholder and how to line up columns neatly in C printf() and format specifiers.

Key takeaways

  • A constant is a value that cannot change while the program runs.
  • Literals are values typed directly into code, such as 42, 3.14, 'A' and "Hi".
  • const makes a typed, read-only variable that must be initialized when declared.
  • #define NAME value is a preprocessor find-and-replace, with no = and no semicolon.
  • Write constant names in UPPER_CASE so readers know they are fixed.
  • Replace unexplained "magic numbers" with named constants to make code clearer and easier to update.

Frequently asked questions

How do you declare a constant in C?

Put const before a normal declaration and give it a value, for example const double TAX_RATE = 0.08;. You can also use the preprocessor: #define MAX_SEATS 150.

What is the difference between const and #define in C?

const creates a typed, read-only variable checked by the compiler. #define is a text replacement done by the preprocessor before compiling, so it has no type and uses no variable.

Can you change a const variable in C?

No. Assigning to a const variable causes a compile error such as assignment of read-only variable. If a value must change, use a normal variable.

Why do C constants use capital letters?

It is a naming convention, not a rule. Writing constants in UPPER_CASE, such as MAX_SEATS, makes it obvious that the value is fixed.

What is a literal in C?

A literal is a fixed value written directly in the code, such as 250, 19.99, 'Z' or "Hello". Its type depends on how it is written, for example 3.14f is a float.