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
Question: Write a C++ program to demonstrate the use of const and #define.
#include <iostream>
using namespace std;
// 1. Using #define to define a constant value
#define PI 3.14159
int main() {
// 2. Using const to declare a constant variable
const int DAYS_IN_WEEK = 7;
// Trying to change a const variable will cause an error
// DAYS_IN_WEEK = 8; // ❌ Uncommenting this line will give an error
cout << "Value of PI using #define: " << PI << endl;
cout << "Days in a week using const: " << DAYS_IN_WEEK << endl;
// Example usage in calculation
float radius = 5.0;
float area = PI * radius * radius; // Using #define constant
cout << "Area of circle with radius " << radius << " is: " << area << endl;
return 0;
}
Question: Write a C++ program to demonstrate the use of #define for constants.
#include <iostream>
using namespace std;
// Using #define to create a constant
#define PI 3.14159
#define GREETING "Hello, World!"
int main() {
// Using the defined constant
float radius = 5.0;
float area = PI * radius * radius; // Using #define constant
cout << GREETING << endl; // Using #define string literal
cout << "Radius: " << radius << endl;
cout << "Area of circle: " << area << endl;
return 0;
}
#define:
#define creates preprocessor constants that cannot be changed during program execution.Examples in code:
#define PI 3.14159 → used for numeric constant#define GREETING "Hello, World!" → used for string constant#define does not have a data type, unlike const.const:
const int DAYS_IN_WEEK = 7;Key Difference:
#define is handled by the preprocessor, not the compiler.const is handled by the compiler, so it has a type and can be used like a normal variable.