Constant or literals in C++ language

Constant or literals are fixed value which can not be reassigned in the program. We have to assign the value of constant at the time of declaring and we can not change its value again. Constant works like as a variable without the value of constant is unchangeable where the value of variable can change any time. They can be of any data type.

Constants are also called literals. literals can be of any data type. Here in this guide we will learn about constant or literals. Now, let’s see some most common types of constants or literals used in C++.

How to define a constant or literals?

There are two way to define a constant in C++ which is given bellow. You can use any one of them.

  • By #define preprocessor
  • By const keyword

Defining constant or literals by #define preprocessor

The bellow program is using #define preprocessor to define a constant. Let’s try to understand the program.

// define constant or literals by #define preprocessor

#include <iostream>
using namespace std;

#define PI 3.1416
#define R 20

int main(){
   float area_of_circle;

   area_of_circle = PI * R * R;
   cout << area_of_circle << endl;

   return 0;



Defining literals by const keyword

const is a keywords in C++ and it is used to declare a constant inside the program. We can assign value of a constant only one time through our program.

// define constant or literals using const keyword

#include <iostream>
using namespace std;

int main(){
   const float PI = 3.1416;
   const int RADIUS  = 15;

   float area = PI * RADIUS * RADIUS;
   cout << area << endl;

   return 0;

Program output:


Now we will see some most common type of constant or literals value which we use inside our program. 

Integer constant or literals

Integer constants are integer numbers. We use three types of integer constant. These are decimal, octal and hexadecimal integer literals. We have to use a prefix 0 for octal and ox for hexadecimal integer.

Suffix u is used for unsigned integer and l for long integer where suffix ul is used for unsigned long integer literals.

const int a = 22;        // decimal integer constant
const int b = 0227;       // octal integer constant
const int hex = 0x6F;     // hexadecimal integer constant
const int sint = 15u;     // unsigned int constant
const int lint = 72l;     // long int constant
const int ulong = 675ul;  // unsigned long int constant

Character constant or literals

Character constants are written inside of a single quote. Although normal characters take 1 byte to the memory, some wide character needs 2 bytes to store. Then we have to store them inside of wchar_t variable and the literal starts with L.

const char myChar = 'P';
const wchar_t lChar = L'g';

Some special characters need \ to print, otherwise they have some other meanings too. This kind of expression is called escape sequence. Here are some mostly used escape sequence code.

Escape sequence Meaning
New Line
Horizontal Tab
Vertical Tab
Form feed

Floating point constant or literals

Floating point numbers are fraction numbers which can be written by a radix point or in exponential form. Both are valid in C++. When we declare a constant with float keyword then it is called floating point literals.

const float height = 5.11;
const float AV = 6023e20;

Boolean constant or literals

Two Boolean literals are either true or false.

String constant or literals

A string is a sequence of character which is written inside the double quotation mark. We can also write any number as string in the same way. We will learn details about string in our string chapter.

Previous pageData types in C++

Next pageKeywords in C++