
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Variable: Const and Volatile
Yes, In C++ both const and volatile keywords can be applied together in a variable. But it is used in situations like a read-only hardware register, or an output of another thread.
In C++, they both are type qualifiers, which are used for different purposes in programming. In this article we will see the use of both keywords in C++.
const Keyword
The const keyword is used to declare the value of a variable as constant, meaning its value cannot be changed or modified later once initialized with const keyword.
Example
In this example PI value is set as constant because the value of pi is an universal constant, which does not required to get changed.
#include <iostream> using namespace std; int main() { const double PI = 3.14; int radius = 4; cout << "Area of the circle is: " << PI * radius * radius; return 0; }
Output
Area of the circle is: 50.24
Volatile keyword
Whereas, volatile keyword is used to declare a variable, whose value may get changed by its own unexpectedly because of any external factors such as hardware, another thread, or an interrupt. So it tells the compiler not to optimize access to that variable and always read its value directly from memory.
Example
In this example the variable buttonPressed is directly linked to the hardware keyboard button, which may unexpectedly cause a change in value of this variable, so to avoid it, we declared it as volatile.
#include <iostream> using namespace std; volatile bool buttonPressed = false; // a variable declared as volatile void checkButton() { while (!buttonPressed) { // here waiting for the button to be pressed } cout << "Button was pressed!" << endl; } int main() { // In real the variable buttonPressed may get changed anytime by hardware checkButton(); return 0; }
Const Volatile Variable
When a variable is declared using both const and volatile together in a variable, then the const keyword will help to prevent the programmer from modifying its value accidentally during the program whereas volatile will prevent it from optimizing access to that variable. As it will tell the compiler that its value may get changed anytime unexpectedly so not to optimize its access to that variable. It is mainly used in embedded systems.
Example
Here is the following example showcasing that the value of variable may get changed because of hardware interruption or any other external factors as it directly interacting with hardware (keyboard button). So here the volatile forces the compiler to read value from memory each time.
#include <iostream> using namespace std; volatile bool buttonPressed = false; // a variable declared as volatile void checkButton() { while (!buttonPressed) { // here waiting for the button to be pressed } cout << "Button was pressed!" << endl; } int main() { // In real the variable buttonPressed may get changed anytime by hardware checkButton(); return 0; }
Output
Hardware status: 1 Hardware status: 1 Hardware status: 1 Hardware status: 1 Hardware status: 1