### Constants/Literals in C++

Constant is refer to fixed values that the program may not alter and they are called literals.

Constants can be of any of the fundamental data types and can be isolated into Integer Number, Floating-Point Numerals, Characters, Strings and Boolean Values.

constants are dealt with simply like ordinary variables aside from that their values can't be altered after their definition.

Defining Constants

There are two straightforward ways in C++ to characterize constants −

• Using #define preprocessor
• Using const keyword.
1. #define preprocessor

Syntax: -
• #define identifier value
Example: -

#include <iostream>
using namespace std;
#define PI 3.14

int main() {
cout << PI;
return 0;
}

3.14
2. const keyword

Syntax: -
• const type variable value;
Example: -

#include <iostream>
using namespace std;

int main() {
const int a = 10;
cout << a;
return 0;
}

10

Integer Literals

An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix.

Integer Literals Prefix
DecimalNothing
Octal 0
intNothing
unsigned int u
long l
unsigned longul

Examples: -
85 //It is Decimal Literals because there is no prefix in number
0 213 //It is Octal Literals because there is 0 prefix in number
0x 1a //It is Hexadecimal Literals because there is 0x prefix in number
30 //It is int Literals because there is no prefix in number
30 u //It is unsigned int Literals because there is u prefix in number
40 l //It is long Literals because there is l prefix in number
50 ul //It is unsigned long Literals because there is ul prefix in number

Floating-Point Literals

A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating point literals either in decimal form or exponential form.

Floating-Point Literals Sign
Decimal Form. (point)
Exponential Form E or e

Examples: -
3 . 14 //It is decimal form Literals because there is .(point)
314 E -2L //It is exponential form Literals because there is E.

Boolean Literals

There are two Boolean literals and they are part of standard C++ keywords −

• A value of true representing true.
• A value of false representing false.

Characters Literals

Character literals are enclosed in single quotes. If the literal begins with L (uppercase only), it is a wide character literal (e.g., L'x') and should be stored in wchar_t type of variable . Otherwise, it is a narrow character literal (e.g., 'x') and can be stored in a simple variable of char type.

There are certain characters in C++ when they are preceded by a backslash they will have special meaning

you have a list of some of such escape sequence codes here−

Escape Sequences Meaning
\\ \
\''
\""
\??
\bBackspace
\fForm Feed
\nNewLine
\tHorizontal Tab
\vVertical Tab

Examples: -
#include <iostream>
using namespace std;

int main() {
cout << "Character /n Literals" ; return 0;
}

Character
Literals

Tech2020

Hi sir say me scope of c++ ni marketing. And what kind of job I can get after learning c++

Raj Kamariya