The constants in ‘C++’ are referred to as fixed values that do not change during execution of a program. There are different types of constants used in C++ are as under:
- Numerical Constants
- Character Constants
- Symbolic Constants
Numerical constants are further categorized into two types:
- Integer Constants
- Real Constants
Integer constants are represented with whole numbers. It requires minimum two bytes and maximum four bytes of memory. The following concept are associated with respect to Integer Constant.
- The numerical constants are represented with numerical numbers. At least one digit is needed for representing the numerical number
- The decimal point or fractional part or any symbols are not allowed or permitted.
- Neither blank spaces nor commas are permitted while representing integer constant
- Integer constant may be either +ve or -ve or may be zero.
- The number without a sign is assumed to be positive.
- Valid Examples: 10,20, +30, -15, etc.
- Invalid Examples: 2.3, .225, $40, 3*^5,etc.
- Integers can be represented in octal or hexadecimal form based on the requirement. Octal has base 8 and hexadecimal 16.
- The octal numbers are 0,1,2,3,4,5,6,7, and hexadecimal numbers are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
- The representation of octal number in C++ would be done with leading digit 0 and for hexadecimal with leading 0X
- For example: 027,037, 0X9, 0Xab,etc.
Real Constants are often known as floating point constants and can be represented in exponential or fractional form. For example, length, height, price, distance are measured in real numbers. The following concepts are associated with Real Constants.
- Decimal point is allowed.
- Neither blank space nor commas are permitted
- Real numbers could be either +ve or -ve.
- Numbers without a sign is assumed to be positive.
- Real constants can be represented in exponential notation, which contains fractional and exponential part.
- For example: 2.4, 3.14, 2.4561 X e+3, etc.
- The real number contains mantissa and exponent. In the above statement 2.4561 is mantissa and +3 is exponent.
The following points must be kept in mind while constructing real numbers in exponential form:
- The real numbers contains mantissa as well as exponent.
- The letter ‘e’ separates the mantissa and exponent and it can be represented either in lower or upper case character.
- The mantissa is either a real number represented either as decimal or as integer.
- The mantissa may be either positive or negative.
- The exponent is an integer which may be either positive or negative.
- Valid examples are: 55.5e3, -2, 5.3e-2, 0.5e-2,etc.
- Also in double data type, the real numbers can be expressed with mantissa and exponent part.