Const keyword in C++

Last Updated : 17 Jan, 2026

The const keyword is used to declare read-only entities such as variables, objects, pointers, function parameters, return types, and member functions. A const variable must be initialized at the time of declaration, and once initialized, its value cannot be modified throughout the program.

how_to_declare_constants
Syntax to declare a const variable in C++
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
Compile-time error due to modifying data through const pointers.

2. Const Pointer to a Non-Const Value

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

int main(){
    int x = 5;
    int z = 6;
    char y = 'A';
    char p = 'C';

    int *const i = &x;   // constant pointer to int
    char *const j = &y;  // constant pointer to char

    *i = 10;
    *j = 'D';

    // i = &z;   // Compile-time error
    // j = &p;   // Compile-time error

    cout << *i << " and " << *j << endl;
    cout << i << " and " << j;
    return 0;
}

Output
10 and D
0x7ffe6474c214 and D

Explanation: In this program, i and j are declared as constant pointers (int *const i and char *const j), which means the address stored in the pointer cannot be changed, but the value at that address can be modified. Therefore, *i = 10 and *j = 'D' are valid and successfully change the values of x and y. However, assigning i = &z or j = &p is not allowed because constant pointers cannot point to a different memory 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
Compile-time error due to assigning addresses to const pointer dereferences.

3. Const Pointer to a Const Value

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

int main(){
    int x{9};
    const int *const i = &x;

    char y{'A'};
    const char *const j = &y;

    cout << *i << " and " << *j;
    return 0;
}

Output
9 and A

Explanation: Here, i and j are constant pointers to constant data (const int *const and const char *const). This means neither the pointer’s address nor the value it points to can be modified. Therefore, attempting to change *i or *j, or to make the pointers refer to another variable, would result in a compile-time error.

Here, the following error will appear: In this 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
Compile-time error caused by modifying data through const pointers.

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

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

Output:

./Solution.cpp: In function 'int main()':
./Solution.cpp:14:17: error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
14 | cout << foo(x);
| ^
| |
| const int*
./Solution.cpp:4:14: note: initializing argument 1 of 'int foo(int*)'
4 | int foo(int *y)
| ~~~~~^

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;
}
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++
#include <iostream>
using namespace std;

class Test{
  int value;

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

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

int main(){
    Test t(20);
    cout << t.getValue() << endl;

    const Test t_const(10);
    cout << t_const.getValue() << endl;
    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
Compile-time error when calling a non-const member function on a const object.

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;

void foo(const int y){
    cout << y;
}

void foo1(int y){
    y = 5;
    cout << '\n' << y;
}

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){
    return y;
}

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
Compile-time error when trying to modify a const function parameter.
Comment