
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is the const Keyword in C++?
We use the const qualifier to declare a variable as constant. That means that we cannot change the value once the variable has been initialized. Using const has a very big benefit. For example, if you have a constant value like the value of PI, you wouldn't like any part of the program to modify that value. So you should declare that as a const.
Objects declared with const-qualified types may be placed in read-only memory by the compiler, and if the address of a const object is never taken in a program, it may not be stored at all. For example,
#include<iostream> using namespace std; int main() { const int x = 10; x = 12; return 0; }
This program would produce an error as we have tried to reassign a const value.
- Related Articles
- What is the difference between #define and const Keyword in C++?
- Function overloading and const keyword in C++
- Difference between readonly and const keyword in C#
- const keyword in Dart Programming
- What is the difference between const int*, const int * const, and int const *?
- Difference between const int*, const int * const, and int const * in C/C++?
- How to use 'const' keyword in JavaScript?
- Difference between const int*, const int * const, and int const * in C
- What is the difference between keywords const and readonly in C#?
- What is volatile keyword in C++?
- What is difference between int and const int& in C/C++?
- What is the use of "is" keyword in C#?
- Difference between const char* p, char * const p, and const char * const p in C
- What is the use of ‘new’ keyword in C#?
- What is the C# equivalent of C++ friend keyword?

Advertisements