Site icon Learn C++

What Are Declared True Constants In Modern C++?

What Are Declared True Constants In Modern C++

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 TrueFalse, 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 TrueFalse, 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:

[crayon-676368314cf12807830419/]

For example:

[crayon-676368314cf1d528368786/]

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:

[crayon-676368314cf1e930306082/]

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.

Types for integer constants

Range of constant (hexadecimal)Range of constant (decimal)TypeAliases
0 $FF0 255ByteUInt8
0 $FFFF0 65535WordUInt16
0 $FFFFFFFF0 4294967295CardinalUInt32FixedUInt
0 $FFFFFFFFFFFFFFFF0 18446744073709551615UInt64
-$80 $7F-128 127ShortIntInt8
-$8000 $7FFF-32768 32767SmallIntInt16
-$80000000 $7FFFFFFF-2147483648 2147483647IntegerInt32FixedInt
-$8000000000000000 $7FFFFFFFFFFFFFFF-9223372036854775808 9223372036854775807Int64


32-bit native integer type

Range of constant (hexadecimal)Range of constant (decimal)TypeEquivalent type
-$80000000 $7FFFFFFF-2147483648 2147483647NativeIntInteger
0 $FFFFFFFF0 4294967295NativeUIntCardinal


64-bit native integer type

Range of constant (hexadecimal)Range of constant (decimal)TypeEquivalent type
-$8000000000000000 $7FFFFFFFFFFFFFFF-9223372036854775808 9223372036854775807NativeIntInt64
0 $FFFFFFFFFFFFFFFF0 18446744073709551615NativeUIntUInt64

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)TypeEquivalent type
-$80000000 $7FFFFFFF-2147483648 2147483647LongIntInteger
0 $FFFFFFFF0 4294967295LongWordCardinal

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)TypeEquivalent type
-$8000000000000000 $7FFFFFFFFFFFFFFF-9223372036854775808 9223372036854775807LongIntInt64
0 $FFFFFFFFFFFFFFFF0 18446744073709551615LongWordUInt64

Here are some examples of constant declarations:

[crayon-676368314cf20918833788/]

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.

Exit mobile version