In this section, we will find out more on C++ constant and literals. In C++, const modifier is used to create constants i.e. variables whose value cannot be changed once they are initialized. Generally constants are written in CAPITAL and usually placed in the top of the program file. Its syntax is as follows:
const type variable = value;
const int PI = 3.14;
You cannot reset the value of a constant. For e.g. the following code snippet will give error as we are trying to change a constant literal's value.
#include <iostream>
using namespace std;
int main()
{
int i,j;
....
const int SPEED_OF_LIGHT = 300000000;
...
SPEED_OF_LIGHT = 250000000 // Error! Cannot change SPEED_OF_LIGHT which is a constant.
...
}
Another way to creare a constant is using the #define preprocessor directive. For e.g.
# define SPEED_OF_LIGHT 300000000
C++ literals are data used for representing fixed point values that we can directly use in the code. For example: 100, 3.1421, 'y', 'n' etc.
Please note that a variable can be assign different values but literals can’t. There are 6 types of literals in C++.
1. Integer literal 🡪 An integer is a fixed point numeric literal. It has no fractional or exponential part. There are three types of integer literals used in C++ programming i.e. decimal (base 10), octal (base 8) and hexadecimal (base 16). Please note that octal literal starts with a 0 while hexadecimal literal starts with a 0x. For e.g.
Decimal literal : -1, 0, 100 etc.
Octal literal : 045, 067, 043 etc.
Hexadecimal literal: 0x8f, 0xa2, 0x721 etc.
Floating point literal 🡪 A floating-point literal is different from integar literal as it has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point
B.literals either in decimal form or exponential form. Decimal form must have the decimal point, the exponent, or both and while the exponential form must have the integer part, the fractional part, or both. The signed exponent is denoted by e or E. For e.g.
3.14134 // Legal
314159E-5L // Legal
620E // Illegal: incomplete exponent
22f // Illegal: no decimal or exponent
.e755 // Illegal: missing integer or fraction
C.Boolean literal 🡪 C++ has two Boolean literals i.e. true which denotes for success/truth and false which stands for failure/falsehood. A programmer usually uses considers the value of true equal to 1 and value of false equal to 0.
D.Character literal 🡪 A character literal is created by enclosing a single character inside single quotation marks. For example: 'a', 'm', 'F', '2', '}' etc. Please note that f for literals begins with L, you should store it in wchar_t type of variable as it is a wide character literal. For e.g.
'y', 'n', L'fu', '(', ']' etc.
E.String literals 🡪 String literals are proper strings that are enclosed in double quotes ("...."). A string literal can have anything such as plain characters, escape sequences, and universal characters. We can break a long line into multiple lines using string literals and separate them using whitespaces. Here are some examples of string literals that are identical strings.
"India is great"
"India \
is great"
"India \
is \
great"
F.Escape sequence 🡪 this type of literal is used for some special operation or to denote a special character that cannot be typed using keyboard. For example, newline (enter), tab, question mark, etc. Some common escape sequences are \n to add new line; \t to add tab space; \b to add a backspace etc.