Different Types of Constants in C++

In this tutorial, we will learn what are the different types of constants in C++.

First let’s see, what is a constant. As the word signifies, a constant is a value that can not be changed throughout the program. We also call them literals. So, the value of a constant remains the same during the entire program. When they are declared, they are also initialized. In C++, there are two ways of declaring a constant :

  • using #define, a preprocessor directive
  • using the ‘const’ keyword
#define pi 3.14;   // using #define
const int k = 78;    //using a 'const' keyword

const datatype identifier = value;   //general syntax

Types of Constants in C++

In C++, we have five types of literals/constants :

  1. Integer literals
  2. Floating point literals
  3. Character type literals
  4. String literals
  5. Boolean literals

Integer literals in C++

An integer literal can be a decimal number, an octal or a hexadecimal number. A decimal literal consists of numbers made up of digits 0-9 with a +ve or a -ve sign. We can also have a decimal point with it, although its not compulsory. An octal number is made up of digits 0-7 with a +ve or a -ve sign. We use the prefix 0. A hexadecimal number is made up of digits 0-9 and A-F. We use the prefix 0x for it.

#include <iostream>
using namespace std;
int main()
{
  const int i = 35;      //using 'const' keyword
  const int o = 043;     //octal constant
  const int h = 0x23;    //hexadecimal constant

  cout << "integer constant is : " << i << endl;
  cout << "octal constant is : " << o << endl;
  cout << "hexadecimal constant is : " << h << endl;

  return 0;
}

Output:

integer constant is : 35
octal constant is : 35
hexadecimal constant is : 35

Floating point or real literals in C++

Floating point or real literals can be written in decimal or exponential form. The difference between a floating decimal and an integer decimal is that an integer decimal can have no decimal point. However, we always write a decimal point with a floating decimal.

#include <iostream>
using namespace std;
int main()
{
  const float f = 283.91;   //floating decimal constant
  const float e = 2.8391E2;  //floating exponential constant

  cout << "floating decimal constant is : " << f << endl;
  cout << "floating exponential constant is : " << e << endl;
  
  return 0;
}

Output :

floating decimal constant is : 283.91
floating exponential constant is : 283.91

Character type literal in C++

Character literals are enclosed in single quotes (‘ ‘) . If we define the literal with a prefix L, then it becomes a wchar_t type literal. Otherwise, it is the simple char literal. We can have a normal char or an escape sequence as the char literal.

#include <iostream>
using namespace std;
int main()
{
  const char c = 'C', ch = 'H';   //char literals
  const char t = '\t';  //escape sequence literal

  cout << "character constants are : " << c << t << ch;
  
  return 0;
}

Output :

character constants are : C     H

String and boolean literals in C++

String literals are enclosed within double quotes (” “). A boolean literal is a fixed boolean value, which are true or false.

#include <iostream>
using namespace std;
int main()
{
  const char S[] = "Hello there !";    //character constant
  const bool b = true;     //boolean constant

  cout << "string constant is : " << S << endl;
  cout << "boolean constant is : " << b << endl;
  
  return 0;
}

Output :

string constant is : Hello there !
boolean constant is : 1

Hope this was helpful. Enjoy Coding!

Also learn :

Mathematical Constants in C++

PI Constant in C++

Leave a Reply

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