# Constants In C++

Explore All About Constants In C++ Along With Its Types.

In this Easy C++ Training Tutorials, we discussed Variables and variable scope in C++ in our previous tutorial. We learned that the value assigned to the variables can be changed throughout the program.

Sometimes, depending on our requirements, we need some values that cannot be changed or modified in the program. However, we cannot guarantee that if these values are assigned to the variables, as nobody will change the values in these variables.

This is because the characteristics of the variable entity itself will not allow values to be constant.

### Overview

Under such situations, we need one entity to which we can assign a value that will remain constant. Even if there is an attempt to change this value, the compiler will generate an error. This entity is called constant/literal. They are also called Symbolic constants as we have a particular name for these constants.

In contrast, the constant values that are assigned to the variables are called literal constants. Constants can be of any data type. Constants in C++ are treated in the same way as variables except that their values do not change.

### Data Types Of Constants

In C++, constants can be of any data type. They are all “named constants” i.e. each of these constants has a name.

Enlisted below are the types of constants in C++:

#### #1) Integer Constants

These are the constants consisting of whole numbers without a decimal point. We can also have some suffixes associated with it depending on whether the number is signed or unsigned or long etc.

Additionally, these constants can have a different base or radix like decimal, octal or hexadecimal. In this case, we specify a prefix to the constant: 0 for octal, 0x for hexadecimal, etc. We do not specify any prefix for decimal constants.

Given below are some examples of the valid integer constant in C++:

0512                       //octal

36                          //decimal

50L                       //long

24U                      //unsigned

Please note that we cannot repeat the prefix or suffix like 50UU as this will make the constant invalid.

#### #2) Floating-Point Constants

Floating-point literals are the literals with a decimal point. These constants can be represented in a decimal form or exponential form. When we use decimal notation, it should contain a decimal point, an exponent or both.

Representation of exponential form should include integer part, fraction, or both. We should represent the signed exponent by e or E.

Some Examples of valid Floating-Point Literals are:

3.142

3142E -5L

1.143

#### #3) Character Literals

These literals are of type character and are usually enclosed in single quotes (‘ ‘). Character literals that begin with ‘L’ are wide-character literals and are stored in wchar_t (wide character) type. Other character literals are stored in a character data type.

Wide character literals are used mostly in GUI programming like MFC or another advanced programming including STL.

Some Examples of Character Literals are:

‘xyz’

L’M’

The above examples of Character Literals are a plain character. There are also character literals known as ‘escape sequences’ which give special meaning to a few characters. They are used to represent actions like newline characters, tabs, etc.

The table given below lists the escape sequences used in C++.

Escape sequenceMeaning
\\\\ character
\’ ‘ character
\”“ character
\?? character
\bBackspace
\fformfeed
\nNewline
\rCarriage return
\tTab horizontal
\vVertical tab
\oooOctal number
\xhh…Hex number (one or more digits)

These escape sequences are mainly used while formatting in C++ and can be used as a combination of one or more escape sequences.

Following C++ program shows the usage of some of these Escape Sequences.

```#include <iostream>
#include <string>

using namespace std;

int main()
{
cout<<"\nC++ program to demonstrate escape sequences";
cout<<"\nHello\tthere\t\'STH\'";
}
```

Output:

C++ program to demonstrate escape sequences

Hello there ‘STH’

As the above code shows, we can use these escape sequences as a combination as well to format the output.

#### #4) String Literal

Unlike character literals, string literals are enclosed in double-quotes (“ “). String literals can also contain simple characters, escape sequences or other universal characters.

Following are some of the valid String Literals.

“Hello, World”
“Hello, /
World”
“Hello” “,” “World”

All the above examples represent the same string but are represented in different ways.

Apart from the data types of literals described above, we also have Boolean literals which use keywords ‘true’ and ‘false’ to represent the constants.

### Defining Constants

In C++ we have two ways of defining constants:

#### #1) Using ‘#define’ Preprocessor Directive

We can define constants using preprocessor directive ‘#define’.

An example is shown below.

```#include <iostream>
#include <string>

#define PI 3.142

using namespace std;

int main()
{
cout<<endl;
cout<<endl;
}
```

Output:

Area of circle: 78.55

Circumference of the circle: 31.42

In the above program, we have two constants defined using “#define” directive, PI and RADIUS. Then inside the main function, we calculate the area and perimeter of a circle using these constants. Note the usage of constants in the program.

#### #2) Using The ‘const’ Keyword

Another way of defining constants is by using ‘const’ keyword with the variable declaration.

const type variable = value;

So if we want to define a constant named “RADIUS”, we do it as follows:

We will use the same program given above to explain this type of constant definition.

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

int main()
{
const float PI = 3.142;
cout<<endl;
cout<<endl;
}
```

As shown in this program, we define two constants using the keyword ‘const’.

We have defined constants in the main function in the above code. Alternatively, we can also define these constants globally, above all the functions. In that case, these constants will have global scope and are called “Global constants”.

Note: As shown in both the programming examples, it’s a good programming practice to define constants in UPPERCASE letters.

### Conclusion

With this, we come to the end of this tutorial on Constants. Constants are advantageous when we want to define certain mathematical values which remain unchanged.

In our upcoming tutorial, we will learn about C++ type qualifiers and storage classes which will allow us to define and use variables in different scopes.