Nearby lessons

10 of 72

C - Constants

Constants is one of the foundational topics in C programming. This lesson explains Constants — All Four Types, The const Keyword 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:

TypeExamplesNotes
Integer constants10, -25, 1000Whole numbers
Real (floating) constants3.14, 0.5, -99.99Numbers 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:

SyntaxMeaning
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
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 const int MAX = 100; // named constant - cannot be changed
6 const float PI = 3.14;
7 
8 printf("Max limit : %d\n", MAX);
9 printf("PI value : %.2f\n", PI);
10 
11 // MAX = 200; // ERROR: cannot modify a const value
12}
Output

Max limit : 100
PI value  : 3.14
      

Program: the four constant types

Example03
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int age = 20; // integer constant
6 float pi = 3.14; // real (floating) constant
7 char grade = 'A'; // character constant
8 char msg[] = "Hello, C"; // string constant
9 
10 printf("Integer constant : %d\n", age);
11 printf("Real constant : %.2f\n", pi);
12 printf("Character constant : %c\n", grade);
13 printf("String constant : %s\n", msg);
14}
Output

Integer constant   : 20
Real constant      : 3.14
Character constant : A
String constant    : Hello, C
      
📝 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 Questions
Progress: 0 / 3