Skip to content
VLSI Mentor

C Part I · Module 2 · Values, Variables, Types and Representation

Constants, Literals and const

You have been writing values directly into source code since Chapter 1.4 — 3, 0xFF, 'A', 0.1, and text in quotation marks — without anyone saying what kind of thing they are. They are literals, and the fact worth knowing is that each one already has a type you never chose, which occasionally matters and is easy to check now that this module has introduced all the types involved. The chapter then turns to const, which is routinely explained as a way of writing constants and is not that. It is a promise about one object: that it will not be modified through the name you are looking at. The difference between those two descriptions is invisible here and decisive in Module 7, which is why the accurate one is used from the start.

Foundation11 min readCPart IFundamentalsTypes

Part I · Module 2 · Chapter 2.11 · Constants, Literals and const

1. Values Written Directly in Source

A literal is a value written directly into your source code. You have used them constantly:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
  3          an integer literal
  0xFF       an integer literal, written in hexadecimal (Chapter 2.4)
  'A'        a character literal (Chapter 2.8)
  0.1        a floating-point literal (Chapter 2.9)
  "text"     a string literal — Module 8's subject

The word covers nothing more exotic than that: a value you wrote down rather than computed.

The fact worth knowing is the one nobody mentions. A literal has a type, chosen by the rules of the language, whether or not you thought about it:

  • 3 is an int.
  • 0xFF is also an int — Chapter 2.4 showed that notation does not change the value, and it does not change the type either.
  • 'A' is an int in C. This surprises people who expect char; it is one of the places where C and C++ differ.
  • 0.1 is a double, not a float. If you want a float literal you have to ask, by writing 0.1f.

Most of the time the type of a literal is exactly what you would have chosen and you never notice. It starts to matter when a literal is large enough that int cannot hold it, or when it meets a value of a different type in one expression — which is Chapter 3.12's subject, and the reason this chapter pins the types down now.

2. A Name for a Value That Should Not Change

Writing a raw number in the middle of code is fine once and a problem when repeated. The number appears in several places, nobody remembers what it means, and changing it means finding every copy.

The fix is to give it a name:

Azvya Education Pvt. Ltd.VLSI Mentor
named, and protected
#include <stdio.h>

int main(void)
{
    const int max_retries = 3;
    const double timeout_seconds = 2.5;

    printf("retries %d, timeout %.1f\n", max_retries, timeout_seconds);
    return 0;
}

Output:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
retries 3, timeout 2.5

const is a qualifier — it attaches to the type and says something extra about it. Here it says this object must not be modified through this name.

The compiler holds you to it:

Azvya Education Pvt. Ltd.VLSI Mentor
the promise being broken
const int max_retries = 3;
max_retries = 5;
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
error: cannot assign to variable 'max_retries' with const-qualified type 'const int'
note: variable 'max_retries' declared const here

Note that this is an error, not a warning — unlike most things in this module, the compiler refuses outright.

Note also the consequence for Chapter 2.3: a const object must be initialized at its declaration. There is no later opportunity, because assignment is exactly what has been forbidden.

3. What const Actually Promises

Here is the wording this curriculum uses, and it is chosen carefully:

const says: this object must not be modified through this name.

Compare it with the phrasing you will usually meet — const means constant — and the difference looks like pedantry. It is not, and two things show why.

First: const is not the same as a value fixed when the program is translated. There are places in C that require a value known at compile time, and a const object does not provide one. This is genuinely different from C++, where the same keyword does more. You will meet the limitation in Module 6, where declaring the size of something needs a compile-time value, and the tool C offers for that is in Chapter 11.6 rather than here.

Second, and this is the one that matters most: in Module 7 you will meet names that refer to an object indirectly. There, const can apply to the indirect access or to the name itself, and those mean different things — one forbids modifying what you reach, the other forbids changing where you are looking. Both are const, and neither is "a constant". The phrase modified through this name is the one that still makes sense when you get there.

4. When to Use It

Use const whenever an object should not change after it is initialized. Two reasons, in order of importance:

It states intent. A reader seeing const int max_retries = 3; knows immediately that this value is fixed for the life of the object, and does not have to scan the rest of the function to find out.

It is checked. Unlike a comment, the compiler enforces it, and as §2 showed the enforcement is an error rather than a warning.

There is no performance argument to make here and this chapter is not going to invent one. const is about stating and enforcing a design decision. That is enough.

The habit worth forming is the same shape as Chapter 2.3's: if an object is not meant to change, say so at the declaration. Adding const later means auditing everything that touches it; adding it at the start means the compiler audits for you.

5. What You Should Be Able to Say Now

  1. A literal is a value written directly in source code.
  2. Every literal has a type you did not choose: 3 and 0xFF are int, 'A' is an int in C, 0.1 is a double.
  3. 0.1f is how you ask for a float literal.
  4. const is a qualifier, attaching to the type and saying something extra about the object.
  5. const means: this object must not be modified through this name — not "this is a constant".
  6. Assigning to a const object is an error, not a warning.
  7. A const object must be initialized at its declaration, because assignment afterwards is forbidden.
  8. const does not give you a value fixed at compile time; C requires a different tool where that is needed.
  9. Use it to state and enforce intent, at the declaration.

6. Exercise

Exercise 1 — name the type. For each literal, say its type: 10, 0x20, 'z', 2.5, 2.5f.

Exercise 2 — predict the diagnostic. Which of these does the compiler reject, and with an error or a warning?

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
const int limit = 10;
limit = 20;
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
const int limit = 10;
int copy = limit;
copy = 20;

Exercise 3 — explain the requirement. Why must a const object be given its value at the declaration? Answer using §2, in one sentence.

Exercise 4 — correct the definition. A colleague writes in a code review: "const makes it a constant, so the compiler can bake the value in." Using §3, say which part is wrong and give a replacement sentence.

Exercise 5 — decide. For each, say whether you would declare it const and why: the maximum number of retries; a running total being accumulated; a timeout value read once at startup; a loop counter.

Exercise 6 — look ahead. §3 says const can attach to different parts of a declaration in Module 7. Without knowing the syntax, describe in your own words what two different promises might be worth making about a name that refers to an object indirectly.

7. Summary

  • A literal is a value written in source, and it already has a type: 3 and 0xFF are int, 'A' is an int in C, 0.1 is a double.
  • 0.1f asks for a float. Without the suffix you get a double.
  • const is a qualifier that says an object must not be modified through this name.
  • That phrasing is deliberate. "Constant" has no way to distinguish the two meanings Module 7 will put in front of you.
  • Breaking the promise is an error, and a const object must be initialized where it is declared.
  • const is not a compile-time constant. Where C needs one, a different tool is required.
  • Say it at the declaration. Stating intent that the compiler enforces costs one word.

One question has been deferred repeatedly through this module. Chapter 2.2 drew an object occupying four bytes and immediately warned you not to memorise the number; Chapter 2.5 explained why C refuses to fix sizes and told you to ask your own machine rather than assume. The last chapter of this module is how you ask.

Standards & specifications

Governing standard
ISO/IEC 9899 (the C language standard)(opens ISO/IEC (JTC 1/SC 22/WG 14) in a new tab)

Defines the C language itself — the type system, expression semantics, object model, storage duration and the behaviour categories a conforming implementation must honour. It is a FAMILY of editions (C89/C90, C99, C11, C17, C23) and this curriculum names the edition wherever a claim depends on one. It does not define any foreign-function interface to a hardware description language, and it does not define platform behaviour: what a memory-mapped access does on real silicon is the implementation's and the device's contract, not this standard's. Working drafts published by WG14 are freely available and are what most engineers read day to day; a draft is not the published standard.

This page also covers RTL structure, verification approach and debugging technique. Those are engineering practice built on the standard, not requirements the standard itself imposes.

Where this fits

Part of the C Programming curriculum.