Constants refer to fixed values that the program may not alter. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Constants are also called literals.
Definition: “A constant value is the one which does not change during the execution of a program.”
Constant uses the secondary storage area. Constants are those quantities whose value does not vary during the execution of the program i.e. constant has fixed value. Constants in every language are the same. For example, in the C++ language some valid constant are: 53.7 , -44.4, +83.65, “Dinesh”, ‘M’, “2013” , “\n” etc.
In C++ language constants are of two types:
1. Numeric Constant
2. Non-Numeric Constant (Character Constant)
These are further sub-divided into more categories.
We’ll be covering the following topics in this tutorial:
Numeric Constant
These have numeric value having combination of sequence of digits i.e. from 0-9 as alone digit or combination of 0-9 with or without decimal point (precision value) having positive or negative sign. These are further sub-divided into two categories as:
(i) Integer Numeric Constant
(ii) Float or Real Numeric Constant
(i) Integer Numeric Constant
An Integer Numeric Constant is a sequence of digits (combination of 0-9 digits without any decimal point or without precision), optionally preceded by a plus or minus sign.
There are 3 types of integer numeric constant namely decimal integer, octal integers and hexadecimal integer.
(a) Decimal Integer Numeric Constant: These have no decimal point in it and are either be alone or be the combination of 0-9 digits. These have either +ve or -ve sign. For example: 1214, -1321, 10,254, -78, +99 etc.
(b) Octal Integer Numeric Constant: These consist of combination of digits from 0-7 with positive or negative sign. It has leading with 0 or 0 (upper or lower case) means Octal or octal. For example: 0317,003, -045 etc.
(c) Hexadecimal Integer Numeric Constant: These have hexadecimal data. It has leading ox, OX, Ox or x, X. These have combination of 0-9 and A-F (a-f) or alone. The letters a-f or A-F represents the numbers 10-15. For example: 0x232, 0x92, 0xACD, 0xAEF etc.
(ii) Float or Real Numeric Constant
Float Numeric Constants consists of a fractional part in their representation; Integer constants are inadequate to represent quantities that vary continuously. These quantities are represented by numbers containing fractional parts like 26.082.
Examples of real constants are: 0.0026, -0.97, 435.29, +487.0, 3.4E-2, 4.5E5
A floating-point constant consists of a sequence of decimal digits, a decimal point, and another sequence of decimal digits. A minus sign can precede the value to denote a negative value. Either the sequence of digits before the decimal point or after the decimal point can be omitted, but not both.
Float Numeric constants are further divided into two parts. One is Mantissa Part and the other is Exponent Part.
(a) Mantissa part: The part without E and having a decimal point is called Mantissa Real part e.g. 45.5, -22.43, 0.5 etc. it is also called without exponent part.
(b) Exponent part: The exponent part has an E within it. It is also called a scientific notation. Here E has base value 10. it computes the power. For example: 4.2xl02 can be written as 4.2E2, 4.2xlO-5 can be written as 4.2E- 5.-Similarly some more valid real numeric constant are as: 54.73 E -4,51.9 E +11 etc.
Character Constant
Character constants have either a single character or group of characters or a character with backslash used for special purpose. These are further subdivided into three types:
(i) Single Character Constant
(ii) String Character Constant
(iii) Backslash Character Constant
(i) Single Character Constant
Single Character constants are enclosed between single quotes(‘). For example, ‘a’ and ‘%’ are both character constants. So these are also called single quote character constant.
For example: ‘a’, ‘M’, ‘5’, ‘+’, ‘1’ etc. are some valid single character constant.
(ii) String Character Constant
C++ supports another type of constant: the string. A string is a set of characters enclosed in double quotes (“). For example: “Punar Deep” is a string.
You must not confuse strings with characters. A single character constant is enclosed in single quotes, as in ‘a’. However, “a” is a string containing only one letter.
For example: “Dinesh”, “Hello”, “2013”, “2013-2020”, “5+3”, “?+!” etc. are some valid string character constant. These are used for printing purpose or display purpose in the C++ program’s output statements. These can also be used for assigning the string data to the character (string) type variables.
(iii) Backslash Character Constants
Enclosing character constants in single quotes works for most printing characters. A few, however, such as the carriage return, can’t be. For this reason, C++ includes the special backslash character constants, shown below, so that you may easily enter these special characters as constants.
These are also referred to as escape sequences. You should use the backslash codes instead of their ASCII equivalents to help ensure portability.
These are used for special purpose in the C++ language. These are used in output statements like cout etc.
Escape Sequences
A list of backslash character constant or escape sequence is as in the below table: