
- 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 different between constant and variable in C++?
Variable and constant are two commonly used mathematical concepts. Simply put, a variable is a value that is changing or that have the ability to change. A constant is a value which remains unchanged.
For example, if you have a program that has a list of 10 radii and you want to calculate the area for all of these circles. To find the area of these circles, you'll write a program that will have a variable that will store the value of PI and this value will not change throughout the program. Such values can be declared as a constant.
In the same example, if you're calculating the area in a loop, you can use the same variable to temporarily store the value of the area and print it and then reuse it for some other calculation. The code for the above will look something like −
float area; const float PI = 3.141; for(int i = 0; i < 10; i++) { area = PI * radii[i] * radii[i]; // Calculate area cout << area; // Print area }
The value of PI remains same throughout the life of this program.
- Related Articles
- What is a constant acceleration and variable acceleration?
- Different ways to declare variable as constant in C and C++
- What is the difference between literal and constant in C++?
- What is the difference between literal and constant in C#?
- Declare variable as constant in C
- What is the difference between constant velocity and uniform velocity?
- What is the difference between Declaring and Initializing a variable in JavaScript?
- What is the difference between a variable and StringVar() of Tkinter?
- What are the differences between a pointer variable and a reference variable in C++?
- In C++ What are the differences between a pointer variable and a reference variable?
- What is Proportionality constant ?
- What is Avogadro’s constant?
- What is Number.NEGATIVE_INFINITY constant in JavaScript?
- Difference Between Identifier and Variable
- What is string constant pool in Java?
