Constants & Literals in C++
As we know that Constants generally refer to the fixed values which cannot be further changed once it is defined. And the values which
are assigned to constant variables are known as
"Literals".
Ex. const int value = 10;
This is an example of a constant expression in which value 5 is referred to as a constant integer literal.
We have also covered the constant
variables in details in our "Variables" chapter. Please also check the same.
Now depending on our predefined datatypes we can Constant Literals can be classified into following types.
- Integer Literals
- Floating Point Literals
- String Literals
- Character Literals
- Boolean Literals
Now let's see these in details.
Integer Literals:
Integer literals are used for the storage of the integer values. There are two ways of representation of integer literals
- Prefix style
- Suffix style
-
Prefix Style: Prefix style generally specifies the base of the integer. There are mainly four types of the prefix representation style.
- Decimal or Base 10: In this representation the first digit should be a non-zero decimal digit and then followed by zero more decimal digits.
Ex. 25, 8, 16, 500, etc. - Hexa-Decimal or Base 16:In this representation we first have to mention "0x" or "0X" and then followed by one or more hexa-decimal digits from
0 to f.
Hexa-Decimal digits - 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e and f.
Ex. 0x52, 0x5c, 0X85ac4, 0xface,0Xdead, etc. - Octal or Base 8: This representation starts with "0" and then followed by one or more octal digits form 0 to 7.
Octal digits - 0,1,2,3,4,5,6,7
Ex. 05410, 025, 010, etc. - Binary or Base 2: This representation starts with "0b" or "0B" and then followed by one or more binary digits 0 or 1.
Ex. 0b0110, 0b111101101, 0B0101, etc.
- Decimal or Base 10: In this representation the first digit should be a non-zero decimal digit and then followed by zero more decimal digits.
-
Suffix Style: In suffix style, depending on the size or charactersitics of the integer type we add the symbol at the end of the decimal.
Ex. 105l, 55ul, 66u, etc.
Suffix representation depending on the datatypes is as follow:Data Type Suffix Example Integer No suffix required const int value = 50 Long Integer l or L const long int value = 50L Long Long Integer ll or LL const long long int value = 235LL Unsigned Integer u or U const unsigned int value = 4545U Unsigned Long Integer ul or UL const unsigned long int value = 5555442245UL Unsigned Long Long Integer ull or ULL const unsigned long long int value = 50505050505050ULL
Floating Point Literals:
Floating point literals are used for the storage of the real value values. Floating point literals consist of integre part, real part, fractional part
and exponential part.It can be represented
by two types:
-
In Decimal form: In this style of representation we have to give a decimal point or exponent part or both of them.
Ex. 105.568, 88558.86868 etc -
In Exponential form: In this style of representation we have to give integer part or fractional part or both of them.
Ex. 1.05568e+2, 8.855886868e+4 etc
String Literals:
String literals are used to store the sequence or group of characters or we can say a word. It will also allow to store the special character and
escape sequences. In the representation
the value should be given inside the double quotes.
Ex. const string value = "Hello!";
const string value = "The Geeky Core!";
Character Literals:
Character literals are used to store a single character. This should be stored inside the single quotes.
Ex. const char value = 'A';
Also, declaring multiple characters inside a single quote will give the warning of 'multi-character constant' and prints only the last character.
For example if we write const char value = 'AB', them it will first give the warning and prints only character 'B'. So to hold multiple characters,
we should either use the
string literals as described above or use the character array and double-quotes instead.
Ex. const char value[] = "ABCD";
We can also use the character literals to store the escape sequences. Some of the escape sequences we can find in C++ are as below:
Escape sequences | Description |
---|---|
\' | single quote |
\" | double quote |
\? | question mark |
\\ | backslash |
\b | backspace |
\f | form feed; new page |
\n | line feed; new line |
\r | carriage return |
\t | horizontal tab space |
\v | vertical tab |
\onn | octal value |
\xnn | hexadecimal value |
A simple code example to describe String and Character literals:-
#include < iostream>
using namespace std;
int main()
{
const char value1 = 'A';
const char value2[] = "ABCD";
const string value3 = "Hello!";
const string value4 = "The\tGeeky\tCore";
const string value5 = "Entering\ninto\nthe\nnewline.";
const string value6 = "I want to say, \"Happy Coding..\"";
cout << value1<< endl;
cout << value2<< endl;
cout << value3<< endl;
cout << value4<< endl;
cout << value5<< endl;
cout << value6;
return 0;
}
The output will be:
A
ABCD
Hello!
The Geeky Core
Entering
into
the
newline.
I want to say, "Happy Coding.."
The above code will explain the usage and syntax of string and character literals.
Boolean Literals:
Boolean literals generally used to store or represent boolean datatype. It mainly store two values:
- True: The keyword used here is "true" and stored value will equals to 1.
- False: The keyword used here is "false" and stored value will equals to 0.
Ex. const bool value = "true"