Nearby lessons
17 of 124C - Constants
Constants is one of the foundational topics in C programming. This lesson explains Constants — All Four Types, The const Keyword and #define with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Constants — All Four Types
A constant is a value that never changes during the program. C has four main types:
| Type | Examples | Notes |
|---|---|---|
| Integer constants | 10, -25, 1000 | Whole numbers |
| Real (floating) constants | 3.14, 0.5, -99.99 | Numbers with a decimal point |
| Character constants | 'A', '5', '\n' | One character in single quotes |
| String constants | "Hello", "C language" | Text in double quotes |
In simple words: a constant is a fixed value that never changes while the program runs — like 20, 3.14, 'A' or "Hello".
The const Keyword — Syntax
To give a constant a name, use the const keyword. The syntax is the same as a variable declaration, but the value cannot be changed later:
| Syntax | Meaning |
|---|---|
const int MAX = 100; | An integer constant named MAX with value 100 |
const float PI = 3.14; | A float constant named PI |
const char GRADE = 'A'; | A character constant named GRADE |
Common Mistake:
const int MAX = 100; MAX = 200; is wrong — the compiler rejects it because a const value cannot be reassigned.Example02
#define — Preprocessor Constant
#define creates a constant at the top of the file, before main(). It is not a variable — the preprocessor simply replaces the name with its value everywhere in the file. No semicolon is used:
In simple words:
#define is a find-and-replace done before compiling — wherever the name appears, the compiler sees the value instead.Example03
Program: the four constant types
Example04
📝 Key Takeaways
- A constant is a fixed value that never changes while the program runs.
- Four types: integer (20), real (3.14), character ('A'), string ("Hello").
- const int MAX = 100; creates a named constant — its value cannot be changed.
- #define PI 3.14 is a preprocessor constant — no semicolon, replaced at compile time.
- Constants keep your code readable and stop accidental value changes.
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3