Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Constant
A constant is a variable whose value can not be changed. It's useful to think of constants as packing containers that hold records that cannot be modified later.
You could consider constants a bag to shop for some books that can not be replaced as soon as placed in the bag.
Literals
Literals are hardcoded values, including the range 5, a man or a woman, or the string, "good day, world!" Numeric literals may be represented in the diffusion of formats (decimal, hexadecimal, binary, floating-point, and so on). A literal always represents the equal value (5 always represents the quantity of 5).
There are five primary varieties of literals:
String Literals
Character Literals
Boolean Literals
Integer Literals
Floating-point Literals
String Literals
A "string literal" is a sequence of characters from the source individual set enclosed in double quotation marks (" "). String literals are used to symbolize a series of characters that, taken together, form a null-terminated string. You must constantly prefix wide-string literals with the letter L.
Example
String1= "CodingNinjas";
String2= "family";
For the above two strings, we use the '+' operator, as shown in the below statement:
This refers to the literals that are used to save a single character within a single quote. To store multiple characters, one needs to apply a character array. Storing multiple characters within a single quote will throw a warning and display just the last character of the literal.
There are certain characters in C++; while a backslash precedes them, they will have a unique meaning, and they're used to symbolize like newline (n) or tab (t). Following is the list of some escape characters:
Escape sequence
Meaning
\\
\ character
\"
" character
\'
' character
\a
Alert or bell
\?
? character
\b
Backspace
\f
Form feed
\n
Newline
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\ooo
Octal number of one to three digits
\xhh . . .
Hexadecimal number of one or more digits
You can also do a free certification of Basics of C++.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Coding\tNinjas\n";
return 0;
}
You can also try this code with Online C++ Compiler
It's a numeric literal that represents only integer kind values. It represents the value neither in fractional nor exponential components. It may be distinctive within the following three methods:
Decimal number (base 10)
It can be described through representing the digits among 0 to 9. for example, 45, 67, etc.
Octal number (base 8)
It is defined as a number wherein 0 is accompanied by digits consisting of 0,1,2,3,4,5,6,7. For Example 012, 034, 0.5, etc.
Hexadecimal number (base 16)
It is described as a variety in which 0x or 0X is observed via the hexadecimal digits (i.e., digits from 0 to 9, alphabetical characters from (a-z) or (A-Z)).
For example:
Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc
Example
include <iostream>
using namespace std;
int main()
{
const int intVal = 20;
cout << "Integer Literal: "
<< intVal << "\n";
return 0;
}
You can also try this code with Online C++ Compiler
Floating-point Literals are used to represent and save real numbers. The real number has an integer element, an actual element, a fractional element, and an exponential element. The floating-factor literals can be stored both in decimal form or exponential form. While representing the floating-factor decimals, one should hold two matters in mind to supply valid literals:
In the decimal form, one should include the decimal point, exponent component, or both. In any other case, it will result in any errors.
In the exponential shape, one needs to include the integer element, fractional part, or both. otherwise, it'll cause an error.
The usage of the const keywords to define constants is as simple as defining variables. The difference is you'll precede the definition with the const keyword.
Example
#include <iostream>
using namespace std;
int main()
{
const int L = 15;
const int B = 15;
int area;
area = L*B;
cout<<"The area of the square " << L<<"x"<<B <<" is: " << area << endl;
return 0;
}
You can also try this code with Online C++ Compiler
How are the constants declared? The const will declare with a specific type value and, #define used to declare user-defined constants.
Number systems whose digits consist of the numbers 0 through 9 are called as? Decimal Number System
Integer literal containing value "F" comes under? Hexa Decimal Number
Key Takeaways
We have learned about constants, literals, and the differences between them. We also learned how to use constants and literals in the programming language and learned about kinds of literals. For more practice, to ace technical interviews, you can visit here.