Introduction to C++ Literals
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 literals in C++ along with its example.
Types of C++ Literals
There are five different types of literals that can be used in C++ as mentioned below:
- Integer Literal: It is used to represent integer constant.
- Float Literal: It is used to represent float constant.
- Character Literal: It is used to represent a single character.
- String Literal: It is used to represent the character sequence(string).
- Boolean Literal: It is used to represent Boolean(true or false).
Each of the above mentioned different types of literals is explained below along with its example code:
1. Integer Literals
Integer literals in C++ represents integer constant value. Integer literals are represented mainly in two ways: prefix and suffix.
Prefix
Prefix in integer literal represents the base in which integer literal is represented.
1. Decimal-literal in base 10: This represents value in decimal (0,1,2,3,4,5,6,7,8,9) digits. The value must start with non-zero decimal digit.
Code:
// For representing decimal integer literal in base 10
const int value= 95;
const int value= 76;
const int value= 7;
2. Binary-literal in base 2: This represents value in binary (0,1) digits. The value must start with a prefix of 0b or 0B.
Code:
// For representing binary integer literal in base 2
const int value= 0b101;
const int value= 0B001;
const int value= 0B1111;
3. Octal literal in base 8: This represents value in octal (0,1,2,3,4,5,6,7) digits. The value must start with a prefix of 0.
Code:
// For representing octal integer literal in base 8
const int value= 0356;
const int value= 0732;
const int value= 0137;
4. Hex-literal in base 16: This represents value in hex (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F) digits. The value must start with a prefix of 0x or 0X.
Code:
// For representing hex integer literal in base 16
const int value= 0xA3;
const int value= 0X38C;
const int value= 0XFFFF;
Suffix
The type in which the integer literal is stored is represented by the suffix.
Suffix | Type |
u or U | unsigned int |
l or L | long int |
ul or UL | unsigned long int |
ll or LL | long long int |
ull or ULL | unsigned long long int |
No suffix | int |
Code:
// 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;
}
Output:
2. Float Literals
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.
- In decimal format, float literals are represented using decimal points and exponent parts.
- In exponential format, float literals are represented using integer and fractional parts.
Syntax:
// For representing float literals using decimal format and exponential format
const float float_leteral1 = 34.876
const float float_leteral1 = 34876E-3
Code:
// 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;
}
Output:
3. Character Literals
Character literals are used to store a single character. A single character is represented with single quotes. 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.
Syntax:
// For representing character literal
const char character_literal = 'A';
//For representing wide character literal
const wchar_t character_literal = L'A';
Code:
// 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;
}
Output:
4. String Literals
String literals are used to represent string (sequence of characters). The string is represented with double-quotes.
Syntax:
// For representing string literal
const char string_literal[] = "mystring";
Code:
// 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;
}
Output:
5. Boolean Literals
Boolean literal represents Boolean constant value. This literal type can take only two Boolean values(true/false).
Syntax:
// For representing boolean literal
const bool bool_literal1 = true;
Const bool bool_literal2 = false;
Code:
// 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;
}
Output:
Conclusion
In this tutorial, different types of C++ literals are explained along with example code. This tutorial will be helpful while working with literals in C++.
Recommended Articles
This is a guide to C++ Literals. Here we discuss five different types of literals in C++ along with examples and appropriate syntax. You can also go through our other related articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses