At times, we may need to define a variable whose value we know cannot be changed.
Ex: A variable to refer to the size of a buffer size.
If we use a normal variable -> It is easy to change the size if needed.
But if we want to prevent code from inadvertently giving a new value to the variable -> It is better to use something that is constant (cannot be changed).
We can make a variable unchangeable by defining the variable’s type as const:
Ex:
1 2 | const int buffSize = 256; // input buffer size buffSize = 512; // error: attempt to write to const object |
Define Constants
We can define the constants in C++ using three ways:
- Using const Keyword
- Using constexpr Keyword
- Using #define Preprocessor
Constant using const Keyword
Syntax:
We first use the const keyword to make sure that our variable is a constant followed by the datatype of the constant variable then proceed with the name of the constant variable and finally give the appropriate value to the constant variable.
Note:
We have to initialize the constant variable at the time of declaration as we cannot modify the value of it later in the program.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> using namespace std; int main() { // declaring a variable int var = 10; // declaring a constant variable const int cons = 24; cout << "Initial Value:" << endl; cout << "var: " << var << endl; cout << "cons: " << cons << endl; // changing the value of both the constants var = 24; cons = 0; cout << "Final Value:" << endl; cout << "var: " << var << endl; cout << "cons: " << cons << endl; return 0; } |
Output:
1 2 3 | ./Solution.cpp: In function 'int main()': ./Solution.cpp:19:10: error: assignment of read-only variable 'cons' cons = 0; |
Constants using constexpr Keyword
The constexpr keyword is similar to const keyword and is also used to declare constants in C++. But the major difference is that the constexpr constants are initialized at compiler time, which is why their value must be known at the compile time. On the other hand, const keyword constants can be initialized at runtime and compile time.
Syntax:
We first use the constexpr keyword to make sure that our variable is a constant followed by the DataType of the constant variable then proceed with the name of the constant variable and finally give the appropriate value to the constant variable.
Ex:
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main() { // defining constant int constexpr hoursIn_day = 24; // printing value cout << "Total hours in a day: " << hoursIn_day; } |
Output:
1 | Total hours in a day: 24 |
The above snippet uses the const and constexpr keywords to define constant the number of hours in a day (hoursIn_day).
Constants using #define Preprocessor
We can also define constants using the #define. Constants created using the #define preprocessor are called “macro constants”. Unlike the other methods, constants defined using this method simply work as an alias for their value which is substituted during preprocessing.
It is the less preferable way to define a constant in C++ due to the lack of type safety.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> using namespace std; // using #define to create a macro #define Side 5 int main() { // using constant const double area = Side * Side; cout << "The area of square with side 5 is "; cout << area; return 0; } |
Output:
1 | The area of square with side 5 is 25 |
We have defined the macro constant Side with the value of 5. We then used it to find the area of the square. The macro constant Side is global meaning it is not restricted to a single function. Also, note that we cannot get user input for the macro constant.
Important Points about Constants
The following are some major properties of C++ constants discussed in the article:
- Constants are the variables with fixed values.
- We have to initialize the constants at the time of declaration and we cannot change this value later in the program.
- const and constexpr can be used to define a constant.
- #define is also used to define a macro constant.