Literals are another name for constants, which are fixed values that a program is not permitted to change. Constants can be any of the fundamental data types and are further broken down into Boolean Values, Characters, Strings, Floating-Point Numbers, and Integer Numericals. Constants are treated the same as regular variables, but they cannot have their values changed once they have been defined.
Data used to represent fixed values are called literals. They can be inserted right into the code. For eg, 9, 11.5, "b," etc.
9, 11.5, and "b" are literals in the above example. This is because these terms won't be able to assign values that are different.
Constants can be defined in C/C++ programs in one of two ways, as demonstrated below:
#define
preprocessor directive: This directive declares an alias name for an existing variable or any value. As illustrated below, we may use this to declare a constant.
#define identifierName value
const
keyword: Defining constants with the const keyword is the same as defining variables; the only difference is that the definition must be preceded by the const keyword using the term const.
const datatype variable_name = value
A decimal, octal, or hexadecimal constant can be an integer literal. The base or radix is specified by a prefix, which can be 0 for hexadecimal, 0 for octal, and for decimal, it will be nothing. Another possible suffix for an integer literal is a combination of the letters U and L, which stand for unsigned and long. The suffix can be in any order and in either capital or lowercase letters. A numeric literal (one connected with numbers) without a fractional or exponential component is called an integer.
Mainly there are 3 types of integer literals in c programming and they are :(link to c tutorial)
In the case of C++ programming octal always starts with 0 and the hexadecimal will always start with 0x
Integer literals in detail with simple examples.
The integer values are represented and stored using integer literals. There are two ways to express integer literals, i.e.
The HexaDecimal base is represented by the prefix 0x. So 20 in HexaDecimal is 32 in Decimal. Hence the value will be 32.
Mainly there are 4 types of integer literals in c++ programming and they are :
For example, 123496098764321LL mainly indicates that the value 12345678901234 is a long long integer with the suffix LL.
Depending on the different data kinds, these are represented in various ways.
#include<iostream>
int main()
{
// constant integer literal is used here
const int intVal = 30;
printf("Integer Literal Will Be:%d \n",
intVal);
return 0;
}
Output:
Integer Literal Will Be : 30
Real numbers are represented and stored using floating point literals. Real numbers are composed of integer, real, fractional, and exponential components. Either decimal or exponential form can be used to hold floating-point literals.
Two considerations must be made when representing floating-point decimals in order to construct valid literals:
Valid Floating literals and invalid floating literals are a few floating-point literals representations.
Example For Valid Floating literals | Example for Invalid Floating Literals |
---|---|
10.541 1.123-20L 10.6E-2 |
126E 1230f 0.e678 |
#include <iostream> using namespace std; int main() { // Real literal const float floatVal = 3.14; cout << "Floating-point literal Will be: " << floatVal << "\n"; return 0; }
Output:
Floating-point literal Will be:3.14
This literal is only offered in C++; it is not available in C. The boolean data types are represented by them.
These have two possible values:
#include <iostream>
using namespace std;
int main()
{
const bool isTrue = true;
const bool isFalse = false;
cout << "isTrue then it is? "
<< isTrue << "\n" ;
cout << "isFalse then it is? "
<< isFalse << "\n" ;
return 0;
}
Output:
isTrue then it is? 1 isFalse then it is? 0
The literal that is used to store a single character inside of a single quote is the subject of this sentence. A character array must be used to store multiple characters. A warning will be issued and just the last character of the literal will be displayed if more than one character is stored within a single quote.
The two representations that result from it are as follows:
For example : // For C++
char chr = 'R';
for example : // For C++
wchar_t chr = L'R';
#include
int main()
{
// constant char literal
const char charVal = 'A';
printf("Character Literal Will Be: %c\n",
charVal);
return 0;
}
Output:
Character Literal Will Be : A
Character literals and string literals are similar. Except that they can store numerous characters and they mainly uses a double quote inorder to store the same.
For Example:
string stringVal = "Learn eTutorials"
#include
using namespace std; int main() { const string str = "Welcome All\nTo\n Learn\t e \t Tutorials"; cout << str; return 0; } }
Output:
Welcome All To Learn e Tutorials