C++ has many different variable types to develop modern applications with modern C++ IDE‘s and compilers. Several different language constructs are referred to as ‘constants’. There are numeric constants and string constants. Every enumerated type defines constants that represent the values of that type. Declared constants are either true constants or typed constants. In this post, we will explain what we mean by a declared constant and what is a true constant which is a kind of declared constant in C++.
What are declared constants in Modern C++?
Several different language constructions are referred to as ‘constants’. There are numeric constants (also called numerals) like 17, and string constants (also called character strings or string literals) like ‘Hello world!’. Every enumerated type defines constants that represent the values of that type. There are predefined constants like True, False, and nil. Finally, there are constants that, like variables, are created individually by declaration.
Declared constants are either true constants or typed constants. There are predefined constants like True, False, and nil. Finally, there are constants that, like variables, are created individually by declaration. These two kinds of constant are superficially similar, but they are governed by different rules and used for different purposes.
What are true constants in Modern C++?
A true constant is a declared identifier whose value cannot change. The syntax for declaring a true constant is:
1 |
const identifier = constantExpression; |
For example:
1 |
const MaxValue = 237; |
declares a constant called MaxValue
that returns the integer 237.
Where identifier
is any valid identifier and constantExpression
is an expression that the compiler can evaluate without executing your program.
If constantExpression
returns an ordinal value, you can specify the type of the declared constant using a value typecast. For example:
1 |
const MyNumber = Int64(17); |
declares a constant called MyNumber
, of type Int64, that returns the integer 17. Otherwise, the type of the declared constant is the type of the constantExpression
.
- If
constantExpression
is a character string, the declared constant is compatible with any string type. If the character string is of length 1, it is also compatible with any character type. - If
constantExpression
is a real, its type is Extended. If it is an integer, its type is given by the table below.
Types for integer constants
Range of constant (hexadecimal) | Range of constant (decimal) | Type | Aliases |
---|---|---|---|
0 $FF | 0 255 | Byte | UInt8 |
0 $FFFF | 0 65535 | Word | UInt16 |
0 $FFFFFFFF | 0 4294967295 | Cardinal | UInt32, FixedUInt |
0 $FFFFFFFFFFFFFFFF | 0 18446744073709551615 | UInt64 | |
-$80 $7F | -128 127 | ShortInt | Int8 |
-$8000 $7FFF | -32768 32767 | SmallInt | Int16 |
-$80000000 $7FFFFFFF | -2147483648 2147483647 | Integer | Int32, FixedInt |
-$8000000000000000 $7FFFFFFFFFFFFFFF | -9223372036854775808 9223372036854775807 | Int64 |
32-bit native integer type
Range of constant (hexadecimal) | Range of constant (decimal) | Type | Equivalent type |
---|---|---|---|
-$80000000 $7FFFFFFF | -2147483648 2147483647 | NativeInt | Integer |
0 $FFFFFFFF | 0 4294967295 | NativeUInt | Cardinal |
64-bit native integer type
Range of constant (hexadecimal) | Range of constant (decimal) | Type | Equivalent type |
---|---|---|---|
-$8000000000000000 $7FFFFFFFFFFFFFFF | -9223372036854775808 9223372036854775807 | NativeInt | Int64 |
0 $FFFFFFFFFFFFFFFF | 0 18446744073709551615 | NativeUInt | UInt64 |
32-bit platforms and 64-bit Windows integer type
32-bit platforms include 32-bit Windows and Android.
Range of constant (hexadecimal) | Range of constant (decimal) | Type | Equivalent type |
---|---|---|---|
-$80000000 $7FFFFFFF | -2147483648 2147483647 | LongInt | Integer |
0 $FFFFFFFF | 0 4294967295 | LongWord | Cardinal |
64-bit platforms integer type excluding 64-bit Windows
64-bit platforms include 64-bit iOS, 64-bit Android, 64-bit macOS and 64-bit Linux.
Range of constant (hexadecimal) | Range of constant (decimal) | Type | Equivalent type |
---|---|---|---|
-$8000000000000000 $7FFFFFFFFFFFFFFF | -9223372036854775808 9223372036854775807 | LongInt | Int64 |
0 $FFFFFFFFFFFFFFFF | 0 18446744073709551615 | LongWord | UInt64 |
Here are some examples of constant declarations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> const int Min = 0; const int Max = 100; const int Center = (Max - Min)/2; const char Beta = (char)225; const std::string Message = "Message"; const std::string ErrStr = " Error: " + Message + ". "; const int ErrPos = 80 - ErrStr.length()/2; const double Ln10 = 2.302585092994045684; const double Ln10R = 1 / Ln10; int main() { return 0; } |
More information about declared constants can be found here
What is constexpr In Modern C++?
Note that In Modern C++, I recommend you to use constexr
to indicate a compile time constant which means it is placed though the compiler. A Constant Expression (constexpr) defines an expression that the value of a variable or function can be used in constant expressions that are evaluated at compile time. The C++11 standard generalizes the concept of constant expressions with a new keyword constexpr
as a declaration specifier. Here is more information about it,
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition