Types of Constants in C++
Here’s where the const declaration in C++ rears its head. A constant value is an explicit number or character such as 1 or 0.5 or ‘c’.
Constant values cannot be changed; that is, they cannot appear on the left-hand side of an assignment statement.
Every constant value has a type. The type of 1 is const int. The type of 0.5 is const double.
The table explains how to declare constant values with different types. For example, 1L is of type const long.
|Type||Declaring a Constant||What It Is|
|int||1||A simple counting number, either positive or negative.|
|unsigned int||1U||A non-negative counting number.|
|short int||—||A potentially smaller version of the int. It uses less memory but has a more limited
|long int||1L||A potentially larger version of the int. It may use more memory but has a larger
|long long int||1LL||A version of the int that is
potentially even larger.
|float||1.0F||A single-precision real number.|
|double||1.0||A double-precision real number.|
|long double||—||A potentially larger floating-point number. On the PC, long double is the native size for numbers
internal to the numeric processor.
|char||‘c’||A single char variable stores a
single character. Not suitable for arithmetic.
|wchar_t||L’c’||A wide character. Used to store larger character sets such as
Chinese ideograms and Japanese kanji symbols. Also known as UTF or
A variable can be declared constant using the const keyword:
const double PI = 3.14159; // declare a constant variable
A const variable must be initialized when it is declared since you will not get another chance in the future — just like a constant value, a const variable cannot appear on the left-hand side of an assignment statement.
It is common practice to declare const variables using all capitals. Multiple words within a variable name are divided by an underscore as in TWO_PI. As always, this is just convention — C++ doesn’t care.
It may seem odd to declare a variable and then say that it can’t be changed. Why bother? Largely because a carefully named constant can make a program a lot easier to understand. Consider the following two equivalent expressions:
double dC = 6.28318 * dR; // what does this mean? double dCircumference = TWO_PI * dRadius; // this is a lot // easier to understand
It should be a lot clearer to the reader of this code that the second expression is multiplying the radius by 2π to calculate the circumference.
The 2011 C++ standard allows you to specify the type of a variable from the type of the initialization value. To do this, declare the variable with the keyword auto rather than with a type:
long function(); auto nIntVar = 1; auto lLongVar = function(); auto dVar = 1.0;
Here the variable nIntVar is declared to be an int because 1 is an int. Similarly, lLongVar is a long because function() is declared as returned a long.
Notice that the type of the initialization value must be known at compile time and once declared, the type of the variable is fixed; you can’t change it later.