08 Feb 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.
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:
- There should be at least one digit in the integer constant.
- There should be no decimal points.
- The integer constant can be positive or negative.
- If there is no sign specified before the integer constant, it is assumed to be positive.
- There should be no commas or blanks in the integer constant.
- 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:
- There should be at least one digit in the real constant.
- There should be a decimal point.
- The real constant can be positive or negative.
- If there is no sign specified before the real constant, it is assumed to be positive.
- There should be no commas or blanks in the real constant.
- 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:
- The e should separate the mantissa and exponent.
- The mantissa can be positive or negative.
- If there is no sign specified before the mantissa, it is assumed to be positive.
- There should be at least one digit in the exponent. It is assumed to be positive by default.
- 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:
- The maximum length of a character constant is one character.
- The character constant should be enclosed with left-pointing inverted commas.
- 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++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Constants in C++ // Code by Studyopedia #include <iostream> using namespace std; int main() { const int a = 50; const float b = 33.25; const char c = 'P'; cout << a << endl; cout << b << endl; cout << c << endl; return 0; } |
Here is the output:
1 2 3 4 5 |
50 33.25 P |
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:
No Comments