C++ Constants

Constants in C++ hold values that don’t change during the program’s execution. There are primarily three types of constants, namely integer, real, and character constants.

Constants in C++

Let us discuss them one by one.

C++ Integer Constants

Integer constants are whole numbers without any fractional and decimal parts. The different rules for constructing integer constants are:

  1. There should be at least one digit in the integer constant.
  2. There should be no decimal points.
  3. The integer constant can be positive or negative.
  4. If there is no sign specified before the integer constant, it is assumed to be positive.
  5. There should be no commas or blanks in the integer constant.
  6. Some examples of integer constants are 42, -45, +45, etc.

C++ Real Constants

Real constants are typically referred to as floating-point constants, and they are indeed categorized based on their representation rather than being specifically called “real” or “fractional.” These constants can be represented as: fractional form and exponential form.

Real Constants: Fractional Form

The different rules for constructing real constants in fractional form are:

  1. There should be at least one digit in the real constant.
  2. There should be a decimal point.
  3. The real constant can be positive or negative.
  4. If there is no sign specified before the real constant, it is assumed to be positive.
  5. There should be no commas or blanks in the real constant.
  6. Some examples of real constants in the fractional form are 32.5, +54.37, -65.46, etc.

Real Constants: Exponential Form

The exponential form is usually used when the real constants are either too large or too small. In this form, the constants are represented in two parts. The part before e is called a mantissa, and the part after e is called an exponent. It uses e or E to denote powers of ten:

The different rules for constructing real constants in the exponential form are:

  1. The e should separate the mantissa and exponent.
  2. The mantissa can be positive or negative.
  3. If there is no sign specified before the mantissa, it is assumed to be positive.
  4. There should be at least one digit in the exponent. It is assumed to be positive by default.
  5. Some examples of real constants in the exponential form are +2.3e5, -6.6e-7, -7.5e3, etc.

C++ Character Constants

The character constants consist of a single character, such as an alphabet, digit, or special character, enclosed within inverted commas.

The different rules for constructing character constants are:

  1. The maximum length of a character constant is one character.
  2. The character constant should be enclosed with left-pointing inverted commas.
  3. Any alphabet, digit, or special character can be a character constant.

Some examples of character constants are ‘K ‘, ‘5 ‘, ‘%’, etc.

C++ Constants – Example

In C++, define constants using the const keyword. Let us see a coding example of constants in C++:

Here is the output:

The above C++ code defines three constants: a (integer), b (float), and c (char). It uses cout to print the values of these constants to the console. The endl is used to insert a new line after each output. Finally, the program ends with return 0;.


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

C++ Enum
C++ Tokens
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading