const Qualifier in C++
In C++, the const qualifier is used to define constants or variables that cannot be modified after they have been initialized. If we attempt to alter a const variable then it results in a compile-time error. This restriction helps in reducing bugs by preventing unintended modifications to variables and it also allows the compiler to optimize code assuming the values will remain unchanged.
Syntax of const in C++
const dataType variableName = value ;
Example
The below example demonstrates how we can use const qualifier in C++.
// C++ program to demonstrate the use of const qualifier
#include <iostream>
using namespace std;
int main()
{
// define a const variable num
const int num = 50;
// print the value of num
cout << "Value of num is: " << num;
// The below line will throw an Error: because we are
// Attempting to modify a const variable num = 50;
//const int num =45;
// cout << "Value of num is: " << num;
return 0;
}
Output
Value of num is: 50
Type Qualifiers in C++
In C++, type qualifiers are keywords that modify the properties of data types, influencing how variables, pointers, or references can be used. These qualifiers enhance variable declarations by providing additional information on their access and usage constraints, ensuring more controlled and secure code behavior. In C++ common type qualifiers include: const, volatile, and mutable, each serving to specify particular attributes or constraints for handling variables and memory.
Contact Us