Module 2 · Variables, Types, Input and Operators — topic 2 of 6
Constants
Values that must not change — π, a tax rate, an array size — should be constants. C offers literals, #define macros, const variables and enumeration constants, each with different behaviour.
Learning objectives
- Write integer, floating, character and string literals correctly
- Choose between #define and const
- Explain what const protects against
Definition
A constant is a value that cannot be modified during program execution. In C it may be a literal (42,'A',3.5), a preprocessor macro (#define), aconst-qualified object, or an enumeration constant.
Introduction
A “magic number” is an unexplained value in code, such as if (age >= 18) or price * 0.18. When the rule changes you must find every copy. Naming such values as constants makes code self-explaining and easy to update.
C gives you several ways to make constants. They look similar but behave differently, and knowing the difference prevents subtle bugs.
Why it matters
Constants communicate intent: MAX_STUDENTS tells the reader far more than 60. They also let the compiler stop you from modifying something by accident — assigning to a const variable is a compile-time error.
Exam questions and interviews often ask the difference between #define and const; the answer reveals whether you understand the preprocessor.
Detailed explanation
Literals: decimal 42, octal 052 (leading zero!), hexadecimal 0x2A, long 42L, unsigned 42U; floating 3.14 (a double), 3.14f (a float), 1e-3; character 'A' (an int value 65 in ASCII); string "hello" (an array of char ending with '\0').
`#define NAME value` is a textual substitution done by the preprocessor. The macro has no type and no memory address; the compiler never sees the name, only the replaced text.
`const type name = value;` creates a real, typed, read-only variable. It obeys scope rules and appears in the debugger. You cannot assign to it after initialisation.
Enumeration constants (enum { RED, GREEN }) name related integer values; they get their own lesson later.
Syntax
- Never put
=or;in a#define— they would be pasted into your code. - A
constmust be initialised when declared, because it cannot be assigned later. - Constant names are conventionally written in capitals.
Code examples
Using both kinds
Area: 19.635
Number literals in different bases
42 42 42 A has code 65
052 is octal (5×8 + 2 = 42) — a leading zero changes the base.
More examples
The macro precedence trap
Bad: 32 Good: 50
Step-by-step explanation
SUM_BAD * 10is replaced by the texta + b * 10before compilation.- Multiplication binds tighter than addition, so it computes
2 + 30 = 32. SUM_GOOD * 10becomes(a + b) * 10, which is5 * 10 = 50.- Lesson: always parenthesise macro bodies that are expressions.
Expected output
Running “The macro precedence trap” prints exactly:
Practical applications
- Header files define limits such as
INT_MAXandEOFas macros. - Firmware defines register addresses and bit masks as constants.
- Financial code stores tax rates as named constants so audits can verify them.
Best practices
- Replace every magic number with a named constant.
- Prefer
constfor typed values; use#definefor array sizes at file scope and for conditional compilation. - Wrap every expression macro in parentheses.
Common mistakes
Semicolon in a #define
Avoid
Prefer
The array becomes int marks[10;]; — a confusing syntax error far from the actual mistake.
Trying to change a const
Avoid
Prefer
Assigning to a const object is a compile-time error. If the value must change, it is not a constant.
Important notes
- A string literal must not be modified:
char *s = "hi"; s[0] = 'H';is undefined behaviour. Usechar s[] = "hi";for a modifiable copy. - In C (unlike C++), a
const intis not an integer constant expression, so it cannot size a file-scope array or be acaselabel.
Key takeaways
- Literals have types:
3.14isdouble,3.14fisfloat,'A'is anint. #defineis text substitution;constis a typed read-only variable.- Parenthesise macro bodies and never end them with a semicolon.
Try it yourself
- 1Write a program that converts 5 km to miles using a named constant for the conversion factor 0.621371.
- 2Predict, then check, what
printf("%d\n", 010 + 0x10);prints.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhy does this fail to compile? `#define SIZE 10;` followed by `int a[SIZE];`
- BeginnerWhat is the main difference between `#define PI 3.14` and `const double PI = 3.14;`?