# C++ Constants/Literals

By | September 29, 2021

In this tutorial of C++, we will discuss C++ constants or literals. Here we have provided a brief definition of literals with appropriate examples.

## C++ Constants/Literals

In C++ the term constants and laterals referred to the fixed values, which may not be changed throughout the program.

In C++ we interchangeably use the term Constants and Literals. On the basis of Data types, we have divided constants into 5 major types, which are   Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

Constants or literals are like normal variables but their values cannot be changed throughout the program and to represent a constant lateral we use the const keyword.

Example:

```#include <iostream>
using namespace std;
int main()
{
const int x = 100; //constant literal x with literal value 5
}```

Behind the code:

In this program, x is the literal variable and 100 is the literal value.

### Types of Literals in C++

• Integer Literals
• Float Literals
• Character Literals
• String Literals
• Boolean Literals

#### 1. Integer Literals

These are used to represent and store the integer values, which can be a decimal, octal or hexadecimal constant.

Example:

```#include <iostream>
using namespace std;
int main()
{
const int val = 10;    // constant integer literal
cout << "Integer Literal: "<< val;
return 0;
}```

#### 2. Floating Points Literals:

With Floating point Laterals, we can represent and store real numbers. The real number includes integer, real, fractional and even exponential. With floating points literal we can deal with decimal numbers, which means we can represent a floating-point literal either with a decimal number or exponent.

Example:

```#include <iostream>
using namespace std;

int main()
{
const float val = 7.14;     // Floating-point literal
cout << "Floating-point literal: "  << val ;
return 0;
}```

#### 3. Character Literal

A character literal is used to represent and store a single character using a single quote. A character could be anything except the escape sequence. A character could be a letter, digit, or any special symbol.

Example:

```#include <iostream>
using namespace std;
int main()
{
const char ch = 'A';     // constant char literal
cout << "Character Literal: "<< ch;
return 0;

}```

#### Boolean Literals:

Boolean literals used to represent and store two values that are true and false. Boolean literal can store boolean data type values.

Example:

```#include <iostream>
using namespace std;
int main()
{
const bool a = true;
const bool b= false;
cout << "a is: "<< a<<"\n";
cout << "b is: "<< b;
return 0;
}```