Preview
Constant in C++ | Literals in C++
What is a constant in C++?
It is a quantity whose value we can’t change during the execution of program. They are of two types.
- Numeric Constant
- Character Constant
Numeric Constant
Numeric Constants contain digits only. They are of two types
- Integer Constant
- Floating point Constant
Integer Constant in C++
Integer Constants can contain numeric values without decimal point in them.
They are of three types.
-
- Decimal Integer Constant
- Octal Integer Constant
- Hexadecimal Integer Constant
Decimal Integer Constant
It contains digits from 0 to 9. So its base is 10
Examples: 6, 45, 5467 etc.
Examples of Invalid Decimal Integer constant are
16,62 //Comma not allowed.
3.6 //Decimal point not allowed
042 //Can’t start with zero
3 33 // Can\t contain space.
$24 //Can’t contain special characters
Octal Integer Constant
It contains digits from 0 to 7. Its base is 8.
Octal Numbers start with 0.
Example: 0123, 06663
Examples of invalid Octal Integer Constants are
49 //Must start with 0.
058 //Can’t contain 8.
Hexadecimal Integer Constant
It contains digits from 0 to 9 and alphabets from A to F. where
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15
Its base is 16. Hexadecimal Number starts with 0X.
Example: 0X23, 0X6f9A
Examples of invalid Hexadecimal Integer Constants are
625 //must start with 0X.
11.5 //Can’t contain decimal point]
Floating point Constant in C++
Floating point constants contain numbers with decimal point. They can be used to represent very small as well as very large quantities.
Real constants can be used in two ways
- Standard Form
- Exponent Form
Standard Form
In this form, floating point number contains digits followed by a dot (.) further followed by digits called fractional part.
Examples: 12.33 , 3.5, -1.5 , +5.0, .55,
Exponent Form
This type of floating point number is represented in terms of power of 10.
Power of 10 is represented by alphabets E or e .
There are two parts of number in exponent form.
- Mantissa: Digits before the alphabet E.
- Exponent: Digits after the alphabet E. It can only contain integers.
Examples
1.45 х 105 can be represented as 1.45E5
3.5590 х 10-2 can be represented as 3.5590e-2
Examples of invalid floating point constants in Exponent form
1.45E2.5 //Exponent Part contains floating point Number
4.55e 4 //Exponent Part contains space between 3 and 4.
Character Constant in C++
Character Constant in C++ can store alphabets, digits, special symbols as well as blank spaces.
They are of three types
- Single Character
- String
- Escape Sequence
Single Character Constant in C++
It contains a single alphabet, digit, special character or space within single quotes.
Examples: ‘A’ , ‘8’ , ‘$’, ‘@’
String Constant in C++
It contains multiple characters within double quotes (Inverted commas “).
String constant always ends with a special character known as null represented as ‘\0’
Examples: “Aman” , “2022” , “$600”, “[email protected]” etc.
“Aman” is a string constant that contains 5 characters as there are four characters in string “Aman” and one character is for null character \0.
Escape Sequence in C++
These are special character constants to represent non graphical characters in C++. Most common non graphical characters include enter key, backspace, tab etc.
They start with a backward slash ( \ ) followed by a specific character.
Most commonly used escape sequences in C++ are:
1) \n
To insert one blank line where it appears.
#include<iostream> using namespace std; int main() { cout<<“Welcome”; cout<<“\nBye “; return 0; }
Output:
Welcome Bye
2) \t
To leave a gap of 8 spaces.
#include<iostream> using namespace std; int main() { cout<<“Welcome \t Bye “; return 0; }
Output
Welcome Bye
3) \b
To delete a character appearing before it.
#include<iostream> using namespace std; int main() { cout<<“Welcome\b Bye “; return 0; }
Output:
Welcom Bye
4) \\
Shows backward slash( \ ).
#include<iostream> using namespace std; int main() { cout<<“Welcome\\ Bye “; return 0; }
Output:
Welcome\ Bye
5 \’
Shows Single Quotes (‘) .
#include<iostream> using namespace std; int main() { cout<<“Father\'s name “; return 0; }
Output:
Father's name
6) \”
Shows Double Quotes (“ ) .
#include<iostream> using namespace std; int main() { cout<<"\"Welcome\""; return 0; }
Output:
"Welcome"
7) \0 (Back Slash followed by zero)
To terminate a string. Nothing after it will be shown.
#include<iostream> using namespace std; int main() { cout<<“Welcome\0 Bye “; return 0; }
Output:
Welcome
Popular Books of Computer Science