We had given an introduction to constants when we had talked about the different types of tokens in C++. Now let us study constants in detail and see how they are used in C++ programs.
First let us recall what constants are.
They are data items that do not change during a program. They are also called literals. So both the terms constants and literals are used interchangeably.
TYPES OF CONSTANTS/LITERALS :
- Integer Literals
- Floating literals
- Character Literals
- String Literals
Now let us see each one of these in detail.
1. INTEGER CONSTANTS
- Integer constants refer to constants that are purely integers.
- They do not have a decimal point or a fractional part.
- For example : -3, 90, -110, 12345 etc.
They are of 3 types:
a) Decimal(Base 10) :
- Sequence of digits that does not begin with 0(zero).
- Eg : 798, -32, 100 etc
- Since the base is 10, each digit(except the first) can take any value from 0 to 9.
b)Octal (Base 8):
- Sequence of digits that begins with 0. Eg: 012,056
- Since the base is 8, each digit can have values only from 0 to 7 but the number has to begin with 0.
c) Hexadecimal (Base 16):
- Sequence of numbers beginning with 0x/0X.
- Eg: 0XA
- Since the base is 16, each digit can have values only from 0 to 15 but the number has to begin with 0X/ox.
In hexadecimal notation, numbers 10-15 are denoted using alphabets.
10-A 11-B 12-C 13-D 14-E 15-F
- A single character enclosed in single quotes constitutes a character constant.
- For example : ‘g’ , ‘z’ etc
- A special thing about characters is that they are represented by their ASCII codes in memory.
- So all characters are converted to their respective ASCII codes when the program is compiled.
- For alphabets, ASCII range is : A – Z : 65 – 90 & a-z : 97 – 122
- DEFINITION : Another class of characters in C++ is non-graphic characters. These are the characters that cannot be typed using the keyboard.
- REPRESENTATION : In C++, non-graphic characters are represented using escape sequences. An escape sequence is a combination of backslash ( \ ) and one or more character.
- This backslash causes an escape from the normal interpretation of the character.
The following table shows commonly used escape characters in C++.
- These are numbers that have a decimal point or a fractional part.
- There must be atleast a single digit after the decimal point.
- They are also called real constants.
- For eg : 127.32, -12.9, 0.009 etc
- Here is a new concept about floating constants.
- They also have an exponential form of representation.
- This is generally used to represent large numbers with a lot of zeroes. But this can be used with smaller numbers as well.
- This includes two things : mantissa and exponent.
- Mantissa is the floating constant and exponent is an integer.
Let us see how we represent floats in exponential form with an example.
- 0.89 = 0.89 * 101 = 0.89 E01
- Here 0.89 is the mantissa and E01 is the exponent.
- Similarly 10.73 = 0.1023 * 102 = 0.1073 * E02 .
- And 0.0023 = 2.3 * 10-3 = 2.3 E-3
- A string is a group of characters.
- When a string is enclosed in double quotes, it becomes a string literal.
- For example: “hello world” is a string literal.
IMPORTANT POINT : Strings in C++ are automatically appended with a ‘\0′ character to signify its end. So in the above example, the size of “hello world” is 12.
We hope you are clear with this topic. Let us know your queries in comments below.