Open In App

Const keyword in C++

Last Updated : 13 Dec, 2025
Comments
Improve
Suggest changes
100 Likes
Like
Report

The const keyword stands for constant. It is used to declare variables, objects, pointers, function parameters, return values, and class methods as read-only, meaning their value cannot be changed after initialization.

  • const variable must be initialized at the time of declaration.
  • Once initialized, its value cannot be modified or reassigned anywhere in the program.
how_to_declare_constants
C++
#include <iostream>
using namespace std;

int main()
{

    const int y = 10;
    cout << y;

    return 0;
}

Output
10

Const Keyword With Pointer Variables

In C++, const can be used with pointers in a few different ways, depending on whether you want the value pointed to or the pointer itself to be constant.

1. Pointer to a Const Value

C++
#include <iostream>
using namespace std;

int main()
{
    int x{10};
    char y{'M'};

    const int *i = &x;
    const char *j = &y;

    // Value of x and y can be altered, they are not constant variables
    x = 9;
    y = 'A';

    // Change of constant values because,
    // i and j are pointing to const-int
    // & const-char type value
    // *i = 6;
    // *j = 7;

    cout << *i << " " << *j;
}

Output
9 A

Explanation: Here, i and j are pointers to const values. The variables x and y themselves can be modified directly, but not through these pointers.

Otherwisethe following error will appear: If we try to modify the value of the const variable.

error

2. Const Pointer to a Non-Const Value

C++
#include <iostream>
using namespace std;

int main()
{
    // x and z non-const var
    int x = 5;
    int z = 6;

    // y and p non-const var
    char y = 'A';
    char p = 'C';

    // const pointer(i) pointing
    // to the var x's location
    int *const i = &x;

    // const pointer(j) pointing
    // to the var y's location
    char *const j = &y;

    // The values that is stored at the memory location can
    // modified even if we modify it through the pointer
    // itself No CTE error
    *i = 10;
    *j = 'D';

    // CTE because pointer variable
    // is const type so the address
    // pointed by the pointer variables
    // can't be changed
    // i = &z;
    // j = &p;

    cout << *i << " and " << *j << endl;
    cout << i << " and " << j;

    return 0;
}

Output
10 and D
0x7ffd0c863f3c and D

Explanation: The const pointer itself cannot point to another address, but you can modify the value stored at its current location.

Otherwise, the following error will appear: The pointer variables are const and pointing to the locations where the x and y are stored if we try to change the address location then we'll face the error.

error2

3. Const Pointer to a Const Value

C++
#include <iostream>
using namespace std;

int main()
{
    int x{9};

    const int *const i = &x;

    // *i=10;
    // The above statement will give CTE
    // Once Ptr(*i) value is
    // assigned, later it can't
    // be modified(Error)

    char y{'A'};

    const char *const j = &y;

    // *j='B';
    // The above statement will give CTE
    // Once Ptr(*j) value is
    // assigned, later it can't
    // be modified(Error)

    cout << *i << " and " << *j;

    return 0;
}

Output
9 and A

Explanation: Here, the const pointer variable points to the const variable. So, you are neither allowed to change the const pointer variable(*P) nor the value stored at the location pointed by that pointer variable(*P).

Otherwise, the following error will appear: Here both pointer variable and the locations pointed by the pointer variable are const so if any of them is modified, the following error will appear:

error3

Const Arguments in Functions

Passing a const argument to a function that expects a non-const parameter causes a compile-time error.

C++
#include <iostream>
using namespace std;

int foo(int *y)
{
    return *y;
}

// Driver code
int main()
{
    int z = 8;
    const int *x = &z;
    cout << foo(x);
    return 0;
}

Output:

output

Additionally, passing const pointer will not result in any error because another pointer is created which also points to the same memory location.

C++
#include <iostream>
using namespace std;

void printfunc(int *ptr)
{
    cout << "Value :" << *ptr << endl;
    cout << "Address of ptr :" << &ptr << endl;
}
// Driver Code
int main()
{
    int x = 10;
    int *const i = &x;
    printfunc(i);
    cout << "Address of i :" << &i << endl;
}

Output
Value :10
Address of ptr :0x7ffff0189b48
Address of i :0x7ffff0189b70

Explanation: The program demonstrates that when a const pointer (int *const i) is passed to a function, the function receives a copy of the pointer, not the original one. Hence, both pointers point to the same value (x = 10), but their addresses are different, showing that the function works with its own local copy.

Constant Member Functions

Class objects can be declared const, preventing modification of their data members. They can only call const member functions, must be initialized at declaration (usually via a constructor), and const functions can be invoked by both const and non-const objects.

There are two ways to declare a constant function:

  • Ordinary Const Function: Declaring a non-member function as const has no real effect; void foo() const is invalid outside a class.
  • Const Member Function: In a class, a member function declared const cannot modify the object’s data members.
C++
// C++ program to demonstrate the
// constant function
#include <iostream>
using namespace std;

// Class Test
class Test
{
    int value;

  public:
    // Constructor
    Test(int v = 0)
    {
        value = v;
    }

    // We get compiler error if we
    // add a line like "value = 100;"
    // in this function.
    int getValue() const
    {
        return value;
    }

    // a nonconst function trying to modify value
    void setValue(int val)
    {
        value = val;
    }
};

// Driver Code
int main()
{
    // Object of the class T
    Test t(20);

    // non-const object invoking const function, no error
    cout << t.getValue() << endl;

    // const object
    const Test t_const(10);

    // const object invoking const function, no error
    cout << t_const.getValue() << endl;

    // const object invoking non-const function, CTE
    // t_const.setValue(15);

    // non-const object invoking non-const function, no
    // error
    t.setValue(12);

    cout << t.getValue() << endl;

    return 0;
}

Output
20
10
12

The following error will if you try call the non-const function from a const object.

error4

Constant Function Parameters And Return Type

A function() parameters and return type of function() can be declared as constant. Constant values cannot be changed as any such attempt will generate a compile-time error. It has the following applications.

  • Prevents accidental changes
  • Better documentation and intent
  • Safer use with references and pointers
  • Performance (avoid costly copies) : For large objects (like big structs or STL containers), copying them can be expensive. Passing by const reference avoids making a copy
C++
#include <iostream>
using namespace std;

// Function foo() with variable
// const int
void foo(const int y)
{
    // y = 6; const value
    // can't be change
    cout << y;
}

// Function foo() with variable int
void foo1(int y)
{
    // Non-const value can be change
    y = 5;
    cout << '\n' << y;
}

// Driver Code
int main()
{
    int x = 9;
    const int z = 10;

    foo(z);
    foo1(x);

    return 0;
}

Output
10
5

1. For const return type: The return type of the function() is const and so it returns a const integer value to us. Below is the C++ program to implement the above approach: 

C++
#include <iostream>
using namespace std;

const int foo(int y)
{
    y--;
    return y;
}

int main()
{
    int x = 9;
    const int z = 10;
    cout << foo(x) << '\n' << foo(z);

    return 0;
}

Output
8
9

Explaination: There is no substantial issue to pass const or non-const variable to the function as long as we are passing it by value because a new copy is created. The issue arises when we try to pass the constant variable by reference to the function whose parameter is non-constant. This disregards the const qualifier leading to the following error:

error5

2. For const return type and const parameter: Here, both return type and parameter of the function are of const types. Below is the C++ program to implement the above approach:

C++
#include <iostream>
using namespace std;

const int foo(const int y)
{
    // y = 9; it'll give CTE error as
    // y is const var its value can't
    // be change
    return y;
}

// Driver code
int main()
{
    int x = 9;
    const int z = 10;
    cout << foo(x) << '\n' << foo(z);

    return 0;
}

Output
9
10

Explanation: Here, both const and non-const values can be passed as the const parameter to the function, but we are not allowed to then change the value of a passed variable because the parameter is const. Otherwise, we'll face the error as below: 

error6

Explore