C++ Constants

In C++ Constants is core part and important in C++ programming language. As the name suggests Constants are fixed value type that means the value of constant never going to change during the execution of the program.

There are key important things about Constants in C++

  • Constants and literal both are same. Sometimes constant is called literal
  • Any data type can be defined as constant or literal.
  • It is considered a best practice to define constants using only upper-case names.

Declaration of Constants

There are two ways to define a constant

  • Const keyword
  • #define preprocessor

Syntax of constant

The following example Constant in detail −

Output:

Constants can be of following types.

  • integer constant.
  • floating constant.
  • character constant.
  • string constant.

Following table shows the constants and its data types with Example

constant typedata type(Example)
integer constantint (53, 762, -478 etc ) unsigned int (5000u, 1000U etc) long
int, long long int (483,647 2,147,483,680)
floating constantfloat (10.456789) double (600.123456789)
character constantchar (Example: ‘A’, ‘B’, ‘C’)
string literalchar (Example: “ABCD”, “Hai”)