Software Developer
The value which is assigned to the constant variable is called literals. The constant value can be named differently as literal. For example, “const int value= 15” assigns constant integer value 15 to value and it is an integer literal. In this article, we will go through an explanation of each of the different types of literal in C++ along with its example.
Five different types of literal can be used in C++ as mentioned below:
Integer literals in C++ represents integer constant value. Integer literals are represented mainly in two ways: prefix and suffix.
Prefix in integer literal represents the base in which integer literal is represented.
// For representing decimal integer literal in base 10
const int value= 95;
const int value= 76;
const int value= 7;
// For representing binary integer literal in base 2
const int value= 0b101;
const int value= 0B001;
const int value= 0B1111;
// For representing octal integer literal in base 8
const int value= 0356;
const int value= 0732;
const int value= 0137;
// For representing hex integer literal in base 16
const int value= 0xA3;
const int value= 0X38C;
const int value= 0XFFFF;
The type in which the integer literal is stored is represented by the suffix.
// include required header files
#include <iostream>
int main()
{
// Body of main()
const int val_decimal = 79; // assigning decimal integer
const int val_hex = 0xA; // assigning hex integer literal
const int val_binary = 0b100; // assigning binary integer literal
std::cout << "Integer literal decimal base: "<< val_decimal << "\n";
std::cout << "Integer literal hex base: "<< val_hex << "\n";
std::cout << "Integer literal binary base: "<< val_binary << "\n";
return 0;
}
Float literals are used to represent real numbers. There are mainly two ways to represent the float literals. It can be stored in decimal format or exponential format.
// For representing float literals using decimal format and exponential format
const float float_leteral1 = 34.876
const float float_leteral1 = 34876E-3
// include required header files
#include <iostream>
int main()
{ // Body of main()
// float literal
const float float_leteral1 = 34.876;
const float float_leteral2 = 34876E-3;
// both float_leteral1 and float_leteral2 are same only representation is different
std::cout << "Float literal 1 value is : "<< float_leteral1 << "\n";
std::cout << "Float literal 2 value is : "<< float_leteral2 << "\n";
return 0;
}
Character literals are used to store a single character. A single character is represented with a single quote. A wide-character literal is represented with wchar_t as mentioned in the below example. To assign the value of wide-character literal L is used before a single character.
// For representing character literal
const char character_literal = 'A';
//For representing wide character literal
const wchar_t character_literal = L'A';
// include required header files
#include "iostream"
int main()
{
// Body of main()
// character literal
const char char_literal = 'H'; //’H’ is assigned to char_literal
std::cout << "Character literal value is : "<< char_literal << "\n";
return 0;
}
String literals are used to represent string (sequence of characters). The string is represented with double quotes.
// For representing string literal
const char string_literal[] = "mystring";
// include required header files
#include "iostream"
int main()
{
// Body of main()
// string literal
const char string_literal[] = "mystring"; //”mystring” is assigned to string_literal
std::cout << "String literal value is : "<< string_literal << "\n";
return 0;
}
Boolean literal represents Boolean constant value. This literal type can take only two Boolean values(true/false).
// For representing boolean literal
const bool bool_literal1 = true;
Const bool bool_literal2 = false;
// include required header files
#include "iostream"
using namespace std;
int main()
{
// Body of main()
// assigning Boolean values to Boolean literals bool_literal1 and bool_literal2
const bool bool_literal1 = true; // assigning true to Boolean literal bool_literal1
const bool bool_literal2 = false; // assigning fasle to Boolean literal bool_literal2
cout << "Boolean literal 1 is : "<< bool_literal1 << "\n";
cout << "Boolean literal 2 is : "<< bool_literal2 << "\n";
return 0;
}