Learn with Yasir

Share Your Feedback

C++ Constants Tutorial | const Keyword in C++


Learn C++ constants with simple examples and explanations. Understand the const keyword, constant variables, and how to use fixed values in C++ programs.

1. What is a Constant?

A constant is a value that does not change during the execution of a program.

Example

const int age = 20;

Here, age is a constant. Its value cannot be changed later in the program.

age = 25;   // Error

2. Why Do We Use Constants?

Constants are useful when a value should remain the same throughout the program.

Common examples

  • Number of days in a week
  • Value of π
  • Maximum marks
  • Tax rate
  • University name or fixed code
  • Conversion values

Example

const int DAYS_IN_WEEK = 7;

Using a constant makes the program:

  • Easy to understand
  • Easy to maintain
  • Less likely to have accidental changes
  • More readable

3. Literal Constants

A literal constant is a fixed value written directly in the program.

Example

int age = 20;

Here, 20 is a literal constant.

double price = 250.50;
char grade = 'A';

Here:

  • 250.50 → literal constant
  • 'A' → literal constant

4. Types of Literal Constants

The common types of literal constants are:

  1. Integer literals
  2. Floating-point literals
  3. Character literals
  4. String literals
  5. Boolean literals

4.1 Integer Literals

Integer literals are whole numbers without a decimal point.

Examples

10
25
100
-50
0

Example:

int marks = 100;

Here, 100 is an integer literal.


4.2 Floating-Point Literals

Floating-point literals contain a decimal point.

Examples

10.5
25.75
3.14
-5.5

Example:

double price = 250.50;

Here, 250.50 is a floating-point literal.


4.3 Character Literals

A character literal represents one character and is written inside single quotation marks.

Examples

'A'
'B'
'5'
'$'

Example:

char grade = 'A';

'A' is a character literal.


4.4 String Literals

A string literal is a sequence of characters written inside double quotation marks.

Examples

"Hello"
"Pakistan"
"Computer Science"

Example:

cout << "Hello World";

Here:

"Hello World"

is a string literal.


4.5 Boolean Literals

Boolean literals have only two values:

true
false

Example:

bool passed = true;

Here, true is a Boolean literal.


5. const Qualifier

The const keyword is used to make a variable constant.

Syntax

const data_type variable_name = value;

Example

const int MAX_MARKS = 100;

MAX_MARKS cannot be changed.

MAX_MARKS = 90;   // Error

Example Program

#include <iostream>
using namespace std;

int main() {

    const int DAYS = 7;

    cout << DAYS;

    return 0;
}

Output

7

Important

A const variable should normally be initialized when it is declared:

const int x = 10;

6. #define Directive

#define is a preprocessor directive used to define a symbolic constant.

Syntax

#define NAME value

Example

#define PI 3.14

Now we can use:

cout << PI;

Complete Example

#include <iostream>
using namespace std;

#define PI 3.14

int main() {

    double radius = 5;
    double area = PI * radius * radius;

    cout << area;

    return 0;
}

Output

78.5

7. const vs #define

const #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

Which should beginners prefer?

For defining typed constants in modern C++, const is generally preferred because it works with C++’s type system.


8. Quick Revision

Constant

A value that does not change during program execution.

const int DAYS = 7;

Literal Constant

A fixed value written directly in the program.

int age = 20;

20 is a literal constant.

Common Literal Types

Integer       → 100
Floating      → 3.14
Character     → 'A'
String        → "Hello"
Boolean       → true / false

const

const int MAX = 100;

#define

#define PI 3.14

⭐ Easy Rule to Remember

Literal = value written directly

const = constant variable with a type

#define = preprocessor symbolic constant

Examples

Example 1: const and define

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;
}

Example: #define


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;
}

Explanation

  1. #define:

    • #define creates preprocessor constants that cannot be changed during program execution.
    • The compiler replaces the name with the value before compilation.
    • Examples in code:

      • #define PI 3.14159 → used for numeric constant
      • #define GREETING "Hello, World!" → used for string constant
      • Key Tip: #define does not have a data type, unlike const.
  2. const:

    • Declares a read-only variable whose value cannot be changed during program execution.
    • Example: const int DAYS_IN_WEEK = 7;
  3. 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.