C++Introduction to C++Learn C++Syntax

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:

For example:

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:

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)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:

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,

What Are Declared True Constants In Modern C++ The C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?