
- 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 are constants in C++?
A constant in C++ is a variable/named value that you just want to assign a value to it once and never let it change again in the execution of that program. For example, if you're writing an application that uses the value of pi in a variable PI. You would never want to let this value change. Declaring this value as a const will prevent it's reassigning throughout the program.
You can define constants in C++ by adding the const qualifier before the declaration of the variable.
Example
#include<iostream> using namespace std; int main() { const int x = 9; x = 0; return 0; }
This will define the constant variable x. But it will throw an error as we are trying to rewrite the value of a constant.
- Related Articles
- What are Enumerated Constants in C++?
- What are C++ Integer Constants?
- What are C++ Character Constants?
- What are C++ Floating-Point Constants?
- What are different types of constants in C++?
- What are Backslash character constants in C language?
- What are different types of constants in C language?
- Are there constants in JavaScript?
- What are the constants with an example in C language?
- What are constants in Kotlin and how to create them?
- What are various inbuilt methods used to access constants database in scipy.constants() module?
- Difference between C++ string constants and character constants
- Mathematical Constants in Python
- Decimal constants in C#
- Constants in Rust Programming

Advertisements