What are Literals in C++
Introduction:
“Constants, often known as literals, are set values in which the program is not allowed to change.”
Literals are the values allocated to a constant variable. Literal is another term for the constant value. Constants can be classified as Floating-Point Numerals, Integer Numerals, Characters, Strings, along with Boolean Values. They can also be any of the fundamental data kinds. Additionally, constants are handled in the same way as ordinary variables, with the exception that once they are defined, their actual values cannot be changed. For example, “const int temp = 20” sets the integer literal temp's constant value to 20. In this article, we will explain many sorts of literals and their examples, as well as literals in C++.
What are Literals?
Working with variables is necessary when using a particular programming language to store data. Literals possess fixed values which represent particular kinds of data and are utilized directly in the code. They are employed as the operands within expressions or for allocating values to variables. Let's examine the various kinds of literals to better grasp their differences.
Types of the Literals in C++:
Five different kinds of Literals exist in C++:
- Integer Literal: An integer constant is represented by an integer literal.
- Float Literal: The representation for the float constant.
- Character Literal: a term for a single character representation.
- String Literal: a representation of a character sequence.
- Boolean Literal: used to symbolize Boolean values (true or false). In C, there is no such literal.
Integer Literal:
The values of the integer constants are represented as integer literals. There are primarily two methods to express them: Suffix and Prefix.
Prefix:
The kind of base from which an integer literal is to be read is indicated by its prefix.
1.Base 10 decimal-literal: This expresses a value in decimal (i.e., in digits 0, 1, 2, 3, 5, 6, 7, 8, 9). A decimal digit that is not zero begins the value.
Example: 75, 58
2.Base 2-binary literal: This expresses value in binary (i.e., 0,1) digits. The value has a prefixed as 0b or 0B at the beginning.
Example: 0b111, 0B101
3.Base 8 octal literal: This expresses a value in octal (i.e., 0, 1, 2, 3, 4, 5, 6, 7) numbers. The value has a 0 prefix at the beginning.
Example: 056, 078
4.Base 16 hex-literal: This shows the value in hexadecimal (i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, a, B, b, C, c, D, d, E, e, F, f). The value is prefixed as 0x or 0X at the beginning.
Example: 0Xa6C, 0x56A
Suffix:
The classification in which an integer literal is to be stored is indicated by its suffix.
Example: 01234567894567LL
01234567894567 represents a long long integer because LL is at the end.
- int: Since integer constants are automatically assigned as int data types, no suffix is needed (ex: 23, 90).
- unsigned int: Character U or u towards the final position any integer constant is known as an unsigned int (ex: 40u or 78U).
- long int: an integer constant with the character L or l towards the end (i.e., 56L or 96l).
- unsigned long int: an integer constant with the character UL or ul near the end (i.e., 23ul or 16UL).
- long long int: an integer constant with the character LL or ll near the end (i.e., 36ll or 65LL).
- unsigned long long int: Character ULL or ull towards the terminus of any integer constant is known as an unsigned long long int (i.e., 72ull or 87 ULL).
Example of integer literal:
Program:
#include <iostream>
using namespace std;
int main()
{
const int value = 37;
cout << "Integer Literal is : " << value << "\n";
return 0;
}
Output:
![What are Literals in C++](https://static.tutorialandexample.com/cpp/what-are-literals-in-cpp1.png)
Floating Point Literal:
Real numbers are represented and stored using these. There are four components to a real number: an exponential, fractional, real, and integer portion. Either decimal or exponential form can be used to hold the floating-point literals. Two considerations must be made when expressing floating-point decimals in order to get a legitimate literal:
Decimal Form: An error will result if the decimal point, exponent portion, or both are not included in the decimal form.
Exponential Form: An error will result if the integer, fractional, or both parts are not included in the exponential form.
By following these guidelines, possible problems in the code may be avoided and floating-point literals can be kept valid and accurate.
- Below are some of the literal floating-point representations:
1.Valid Floating point Literals:
10.136
1.251-10L
10.6E-3
2.Invalid Floating point Literals:
156E
1750f
0.e256
Example:
Program:
#include <iostream>
using namespace std;
int main()
{
const float floatValue = 4.14;
cout << "Floating point literal is : " << floatValue << "\n";
return 0;
}
Output:
![What are Literals in C++](https://static.tutorialandexample.com/cpp/what-are-literals-in-cpp2.png)
Character Literal:
For storing a single character described in single quotes (""), use character literals. Generally, a character array is used to store many characters. Usually the last character associated with the literal is taken into consideration, therefore attempting to save more than one character inside a single quotes will produce a warning.
Character literals are represented in two main ways:
A.char Type: Regular character literals or narrow character literals are stored in this form. It works with both C++ as well as C. These kinds of character literals are frequently utilized in both languages they can include a single character inside single quotes.
Example:
char character = ‘J’;
B.wchar_t type: The wchar_t type, which is exclusively available in C++, is used to express wide-character literals as opposed to the char type. A character denotes a wide-character literal (a literal that should be saved as a "wchar_t") whenever it is preceded by the "L" prefix. This is especially helpful for handling character sets as well as wide characters, which take up more storage space compared to the regular narrow characters.
Example:
wchar_t character = L'J';
Example:
Program:
#include <stdio.h>
int main()
{
const char character_Value = 'T';
printf("Character Literal is : %c \n", character_Value);
return 0;
}
Output:
![What are Literals in C++](https://static.tutorialandexample.com/cpp/what-are-literals-in-cpp3.png)
String Literals:
String literals are surrounded in double quotes (“”) and do, in fact, function as containers for numerous characters. String literals are useful for expressing text as well as larger amounts of data because they can contain character sequences, unlike character literals.
Special characters, including escape sequences (represented by backslashes, like ‘\n’ to represent a newline or ‘\t’ for a tab), can also be handled by string literals. Because of its adaptability, a string can include a variety of characters along with control codes.
Example:
string string_value = "JavaTpoint"
Example:
Program:
#include <iostream>
using namespace std;
int main()
{
const string strng = " Welcome \n To \n C++ \t Progamming \t class";
cout << strng;
return 0;
}
Output:
![What are Literals in C++](https://static.tutorialandexample.com/cpp/what-are-literals-in-cpp4.png)
Boolean Literals:
Boolean data types are represented via boolean literals, which are unique to C++. Two values are possible for these literals:
true: This is a unique boolean value that denotes the state of being "True." It shouldn't be equated to the integer 1.
false: This is a unique boolean value that denotes the "False" value and shouldn't be interpreted as equivalent to the integer 0.
While dealing with the boolean data types in C++, boolean literals are crucial for creating concise, understandable code that is more expressive as well as more readable.
Example:
Program:
#include <iostream>
using namespace std;
int main()
{
const bool is_True = true;
const bool is_False = false;
cout << "isTrue? " << is_True << "\n";
cout << "isFalse? " << is_False << "\n";
return 0;
}
Output:
![What are Literals in C++](https://static.tutorialandexample.com/cpp/what-are-literals-in-cpp5.png)
Frequently asked FAQ’s on C++ Literals:
1.Define literals with an example?
Yes, literals are constant values that may be allocated to variables and are directly integrated into the code of a program. The example you provided, "int count = 0;," clearly illustrates this idea.
The literal "0" within this sentence indicates the value that has been allocated to the integer variable "count," which in this case is zero. "int count" corresponds to the declaring of an integer variable with the name "count." Programming's foundational elements literals aid in initializing variables and expressing fixed values that lie within the code.
2.What kinds of numeric literals are there?
Numerical literals include the following examples: 1, 2, 3.4, -5, -6.78, and +9.10. In scientific notation, approximate-value numeric literals can be expressed using an exponent and mantissa.
3.Determine the size difference between string literals and character constants?
The distinction between string-literals and multi-character character constants—which are frequently used to write octal, hexadecimal, or ASCII numbers—is that the latter is represented as an array of char while the former is viewed as an integer (of the int type) with implementation-dependent values.
Conclusion:
We have covered the types of C++ literals in this post. Working with variables is necessary when using a particular programming language to store data. Literals have predetermined values that represent particular kinds of data and are utilized directly in the code. They are employed as operands within equations or for allocating values to variables. There exist several categories of literals.