constant and their types in C++

In a way, the constant is used for constant values. When we declare a variable in constant variables, So these variables values never ever change during execution or running of a program i.e. their value remains fixed. When you declare the constant variable, we have to initialize it at the same time.

Download this as PDF format header file constant and their types in C++

some important facts about constant.

Constant can be any type of data type.

  • It has to be initialized at the time of the declaration of the Constant.
  • Once the value is assigned to the constants it is fixed. This cannot be changed later.
  • We can use constants in two ways, first by using the “const” keyword and second by using “#define” pre-processor.
  • They are also called literals.

defining constant in C++

  • To declare a variable as constant, we need to add the “const” keyword before the variable.
const data-type variable-name = value;
  • we can also use #define Pre-processor to define a constant,
#define data-type variable-name value;

Let’s consider with an example here,

using “const” keyword

#include <iostream>
using namespace std;

 int main()
     const int FIRST = 10;
     const char NEXTLINE = '\n';
      const float SECOND = 0.1; 
  return 0;  



using #define Pre-processor

#include <iostream>
using namespace std;

 int main()
     #define FIRST 10;
     #define char NEXTLINE  '\n';
     #define float SECOND  0.1; 
  return 0;



it is not good programming to use #define pre-processor to define a constant in C++.

Let’s more about constant,

constant type in C++

  •  numeric constant
  • characters constant

But they also have many types. You can understand this from the diagram given below-


numeric constant

It contains all kinds of numeric values, there are 2 types of numeric constant-

  • integer constant
  • floating constant

an integer constant is 3 types

  • decimal constant
  • Octal constant
  • Hexa-Decimal constant

decimal constant

it contains all the normal numeric values (both positive and negative). Whose base is 10 (0-9). like

octal constant–

It has base 8 (0-8). like-

const int x = 012; const int y=0200; const int z = 011;

hexadecimal constant –

it has Base 16 (0-9 and A-F).

const int x = 0x11; or const int y=0x2f1;

floating constant

It contains all the floating numeric type value. like-

const int x = 0.5;const int y= 0.10; const int 4.53;

Characters Constant

There are character values in it. Such as a name or “a” alphabet. It is divided into two parts-

  • character constant
  • string constant
Character Constant

There is a single character in it. In character constant, single quotes are used in the character, such as a character or alphabet

const char x = '5';
const char y = 'x';
const char z = ' '; // null
String Constant

There is a character of sequence or sentence. like someone’s name string constant is declared in double-quotes like-

const char name[ ] = "LearnC++";
const char y = ""; // null string
const char x[] = " " //  space
const char z[20]= 'learn C++ ';

we can declare a constant variable both locally and globally.

previous- character set, identifier and comments in C++
next- variables and their types in C++

Did You understand well?

Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version