Constants: A constant is a value that does not change
during the program execution. As its name implies it is a constant or fixed
value. Constants are declared with the ‘const’ keyword. Constants in every
language remain same. In some languages, constants are also known as literals.
The constants can be classified as integer constant, floating constant,
character constant and string constant. For example: const data_type var;
Integer Constant: An integer is a whole number without decimal point. An integer may be
either 0-9 digits or the combination of 0-9. Integer can be further divided
into three different number systems: decimal (base 10), octal (base 8) and
hexadecimal (base 16).
Octal: 1) It consists of numeric range 0-7 or combination of 0-7 numbers.
2) The first digit must be 0 in order to identify the constant as an
octal.
3) For example: 00, 010, and 0200.
Decimal: 1) It consists of any combination of 0-9 digits. It is the most
commonly used number system.
2) It is normal counting number of mathematics. The first digit needs
not to be zero.
3) For example: 0, 8, 156, 25378 etc.
Hexadecimal: It consists of 0-9 digits and alphabets ‘a’ to ‘f’ or ‘A’ to ‘F’. Here
‘a’ to ‘f’ represents the decimal values 10 to 15. ‘A’ or ‘a’ is 10, ‘B’ or ‘b’
is 11 and so on.
2) A hexadecimal number must begin with 0x or OX.
3) For example: 0x0, 0x10, 0x265 etc.
Rules for integer constants: 1) it may be either positive or negative.
2) It must not have any decimal point.
3) No commas or blank spaces are allowed in an integer.
4) It must have at least one digit.
Floating Constant: A floating point constant has a real value. It is a numeric value with
decimal point. It may be either of single precision or of double precision
type. It may be written in two forms called the fraction form and exponent
form.
Fraction form: In the fraction form, the value is represented as a whole
number followed by a decimal point. For example: 130.35, -0.67
Rules for fraction form: 1) a decimal point should be present.
2) No commas or spaces are allowed.
3) It may be +ve or -ve but default is +ve.
Exponential form: In the exponential form a floating point constant has
a mantissa part and exponent. The mantissa part and exponent part should be
separated by e or E. For example: a number 234.56 can be written as 2.3456e2 or
2.3456e+2 i.e. 2.3456*10^2 Here 2.3456 represents mantissa part and the part
after 'e' is exponent part, which has a base 10.
Rules: 1) the mantissa part can be written in positive or negative.
2) Spaces and commas are not allowed.
3) Letter e can be written in uppercase or lowercase.
Character constant: A character constant is a single alphabet, single digit
or single symbol which is enclosed in single quotes (' '). Every character
constant has a unique ASCII value. For example ASCII value of 'A' is 65. The
maximum length of a character constant is only one character. For example: 'a',
'&', '2' etc.
A character can be any ASCII character, printable or not printable
from values -128 to 127. (But only 0 to 127 are used.) Control characters i.e.
non printable characters are put into programs by using a
backslash \ and a special character or number. The characters and
their meanings are:
\b
backspace
BS
\f
form
feed FF (also clear screen)
\n
new
line NL (like pressing return)
\r
carriage
return CR (cursor to start of line)
\t
horizontal
tab HT
\v
vertical
tab (not all versions)
\"
double
quotes (not all versions)
\'
single
quote character '
\\
backslash
character \
These are some certain character constants that start with backslash
(\). Such characters are known as escape sequence character. There are
predefined in compile and used with output statements. These characters are non
printable and have some specific tasks.
String constant: A string constant is a group of characters, which is enclosed in double
quotes. So it is also called double quotes character constant. The characters
enclosed in double quotes may be alphabet, digits, special symbols, escape
sequence and spaces. For example: "This is my pen", "hi...\ this
is Vijraj Bhargav", "My mobile number is 9872871471".