
- 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
Why do we use const qualifier 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 of 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
- Why do we use restrict qualifier in C++?
- Why do we use a volatile qualifier in C++?
- Const Qualifier in C
- Why do we use "use strict" in JavaScript?
- Why do we use random.seed() in Python?
- Why do we use interfaces in Java?
- Why do we use pandas in python?
- Why do we use brackets in BODMAS?
- Why do we Use JavaScript in HTML?
- Why do we use Convex Mirrors ?
- Why do we use JSON.stringify() method in jQuery?
- Why do we use DOCTYPES in HTML document?
- Why do we use modifiers in C/C++?
- Why do we use internal keyword in C#?
- Why do we use comma operator in C#?

Advertisements