C++ Constants


In the C++, the fixed value for the whole program is termed as constants. The constant is similar to the variables, the only difference is the variable may have different values throughout the program, but the constant values remain fixed for the whole program. Constants are also termed as Literals.

Like variables, constants can be also of different data types such as integer (int), character (char), floating point (float), boolean (bool), null (void), strings etc. The constants can be user defined type also. Again, the integer numeric constants are classified as decimal, hexadecimal, octal etc.

Integer Constant: the integer numeric constants are consist of a sequence of numeric value ( 0 to 9 ) and with a plus or minus sign (signed / unsigned). These can be categorized as follows:

  • Decimal integer: these are the combination of numeric values from of 0, 1, 2, ……., 9. These have the radix or base 10. The decimal integer either positive or negative.
  • Octal integer: these are the combination of numeric values of 0, 1, 2, ….., 8. It has the base 8. It may be positive or negative.
  • Hexadecimal integer: these are the combination of digits from 0 to 9 and letters from A to F. It has the base 16.

The integer constants are written with a prefix and a suffix. The prefix is used to specify the base of the constant such as:

  • Nothing for decimal
  • 0 or o for octal
  • Ox, ox, OX, x or X for hexadecimal

The suffix is used to specify the modifier of the integer constant such as:

  • U or u for unsigned
  • L or l for long

These modifiers can be used as a combination or single and may be of uppercase or lowercase and can be of any order. Some of the valid examples of integer constant are: 1214, 037, ox23F, 215u, 30ul etc.

Floating point constant: the floating point constants are used to specify real values using decimal form or exponential form.

  • In the decimal form, the numeric constant is the combination of decimal value, fractional value or both without the exponent part.
  • In the exponential form, the integer constant is the combination of integer value, the exponent or both. The exponent form is also called as scientific notation.

The floating point constants are also specified with the suffix such as:

  • F or f for float
  • L or l for long double

Some of the valid examples of floating point constants are: 1.5687, 9.0e56, 510E-7L etc.

Character constants: the character constant consist of either a single character, set of characters or a character with backslash for some special purpose. Based on this, the character constants are divided into 3 types:

  • Single character constant: in the single character constant, only a single character is defined enclosed in single quotes ( ‘ ). For example: ‘a’, ‘%‘; etc.
  • String character constant: the string character constant contains a group or set of letters enclosed in the double quotes (“). For example: “this is a string”, “character” etc. here, “a” is also a string constant where it is a word containing only one character.
  • Backslash character constant: the backslash character constants are written with a backslash ( \ ) for performing special task. These are also referred to as escape sequence. Some of the escape sequences along with the meaning are:

C++ Constants



Boolean literals: The Boolean literals are used to represents true or false value of the constants. It has 2 inbuilt keywords as:

  • true for representing true value
  • false for representing false value

The C++ constants are declared and defined at the same time. There are various ways in the C++ to convert a variable into a constant. These are:

Using macros: the C++ variables can be defined as constants by using the macros i.e. by defining a variable using pre-processor (#) and define keyword. It has the syntax as follows:

For example:

Here, the variable NUM has the fixed value 50. The macros are the simplest way of defining constants as their values can be redefined in the program by using other macros.

Using const keyword: The const keyword can also be used to define a variable as constant as the const keyword tell the compiler that this variable value cannot be changed during the compile time. It has the syntax as follows:

For example:

Here, the integer type constant a is assigned with the fixed value 10. Using enum keyword: the enum keyword can also be use to define a set of variables with fixed value starting from 0 and incremented by 1. It has the following syntax:

For example:

Here, the enumerated data type is defined which has the constant value 0, 1, 2 and 3 for jan, feb, mar and apr. it is equivalent to:

We can also assign other values to jan, feb, mar and apr as:

Using constexpr keyword: The constexpr keyword is used to declare a variable as a guaranteed constant. But, in this keyword the initialized value must be constant also. It has the syntax as:

For example:

Here, the identifier a must be constant also. Hence, before defining it we must initialized a as constant. Hence, the proper initialization is:

 

Close Menu