Learn C++ constants with simple examples and explanations. Understand the const keyword, constant variables, and how to use fixed values in C++ programs.
A constant is a value that does not change during the execution of a program.
const int age = 20;
Here, age is a constant. Its value cannot be changed later in the program.
age = 25; // Error
Constants are useful when a value should remain the same throughout the program.
const int DAYS_IN_WEEK = 7;
Using a constant makes the program:
A literal constant is a fixed value written directly in the program.
int age = 20;
Here, 20 is a literal constant.
double price = 250.50;
char grade = 'A';
Here:
250.50 → literal constant'A' → literal constantThe common types of literal constants are:
Integer literals are whole numbers without a decimal point.
10
25
100
-50
0
Example:
int marks = 100;
Here, 100 is an integer literal.
Floating-point literals contain a decimal point.
10.5
25.75
3.14
-5.5
Example:
double price = 250.50;
Here, 250.50 is a floating-point literal.
A character literal represents one character and is written inside single quotation marks.
'A'
'B'
'5'
'$'
Example:
char grade = 'A';
'A'is a character literal.
A string literal is a sequence of characters written inside double quotation marks.
"Hello"
"Pakistan"
"Computer Science"
Example:
cout << "Hello World";
Here:
"Hello World"
is a string literal.
Boolean literals have only two values:
true
false
Example:
bool passed = true;
Here, true is a Boolean literal.
const QualifierThe const keyword is used to make a variable constant.
const data_type variable_name = value;
const int MAX_MARKS = 100;
MAX_MARKS cannot be changed.
MAX_MARKS = 90; // Error
#include <iostream>
using namespace std;
int main() {
const int DAYS = 7;
cout << DAYS;
return 0;
}
7
A const variable should normally be initialized when it is declared:
const int x = 10;
#define Directive#define is a preprocessor directive used to define a symbolic constant.
#define NAME value
#define PI 3.14
Now we can use:
cout << PI;
#include <iostream>
using namespace std;
#define PI 3.14
int main() {
double radius = 5;
double area = PI * radius * radius;
cout << area;
return 0;
}
78.5
const vs #defineconst |
#define |
|---|---|
Uses the const keyword |
Uses #define |
| Has a data type | Does not have a C++ data type |
| Creates a constant variable | Creates a preprocessor macro |
| Checked by the compiler | Replaced by the preprocessor |
Example: const int X = 10; |
Example: #define X 10 |
For defining typed constants in modern C++, const is generally preferred because it works with C++’s type system.
A value that does not change during program execution.
const int DAYS = 7;
A fixed value written directly in the program.
int age = 20;
20 is a literal constant.
Integer → 100
Floating → 3.14
Character → 'A'
String → "Hello"
Boolean → true / false
constconst int MAX = 100;
#define#define PI 3.14
Literal = value written directly
const = constant variable with a type
#define = preprocessor symbolic constant