What is C++ Constants with Examples

In this C++ Constants Tutorial, we talked about variables and the range of variables in C++. Variable values can fluctuate during a program.

Sometimes we need program values that can’t be modified. We can’t ensure these values are put into variables since nobody will modify them.

Overview

In this situation, we need one thing to which we can give a value that won’t change. The compiler will throw an error if this value is tried to be changed.

The name for this thing is constant/literal. We have a special name for these constant expressions, and we call them “Symbolic constants.”

Literal constants, on the other hand, are the values that are always given to variables. Constants can be made up of any data. In C++, constants are just like variables, except that their values never change.

Data Types Of Constants

Constants in C++ can be any data type. All of them are “named constants,” which means that each of them has a name.

Here is a list of the different kinds of constants in C++:

1.Integer Constants

• These constants are whole numbers and don’t have a decimal point. We can also add some suffixes to it, depending on how signed or unsigned the number is, how long it is, etc.

These constants can also have different bases or radices, such as decimal, octal, or hexadecimal. In this case, we tell the constant what kind of number it is: 0 for octal, 0x for hexadecimal, etc. We do not specify any prefix for decimal constants.

Some examples of valid integer constants in C++ are given below:

2.Floating-Point Constants

• Literals with a decimal point are called “floating-point literals.” You can write these constants in either decimal or exponential form.

When we use decimal notation, it should have a decimal point, an exponent, or both. Exponential form should be shown with an integer literal part, a fraction part, or both. We should write e or E for the signed exponent.

Here are some examples of correct Floating-Point Literals:

3. Character Literals

• These literals are of type character and are usually surrounded by single quotes (‘ ‘). Wide-character literals are character literals that start with “L” and are stored in the wchar_t (wide character) type.

A character data type stores other character literals. Most of the time, wide character literals are used in GUI programming, like MFC or STL, or another advanced programming.
The following are some examples of character literals:

‘xyz’ , L’M’

The Character Literals shown above are easy to understand. There are also things called “escape sequences” that give certain characters a special meaning. They are used to show things like tabs, newline characters, etc.

The C++ escape sequences are shown in the table below.

The character set member used during run time is the value of an escape sequence. During preprocessing, escape sequences are turned into something else.

For example, the letter V is the value of the escape sequence x56 on a system that uses ASCII character codes. On a computer that uses EBCDIC character codes, the letter V is represented by the escape sequence xE5.

ExampleProgram
``````#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 also use a combination of these escape sequences to format the output.

4.String Literal

• String literals are enclosed in double quotes (” “), while character literals are not. String literals can also be made up of simple characters, escape sequences, and other characters that are used everywhere.

Here are some of the String Literals that can be used.

We also have Boolean literals, which use the keywords “true” and “false” to represent the constants.

Defining Constant

Constants with names must be given a value when they are defined.

1. Using ‘#define’ Preprocessor Directive

Example Program
``````#include <iostream>
#include <string>

#define PI 3.142

using namespace std;

int main()
{
cout<<endl;
cout<<"Circumference of circle: "<<2*PI*RADIUS;
cout<<endl;
}``````
Output

Area of the circle: 78.55
Diameter of the circle: 31.42

In the above program, we use the “#define” directive to define the preprocessor’s two constants: PI and RADIUS. Then, using these constants, we calculate the area and perimeter of a circle inside the main function. Take notice of how constants are used in the program.

2. Using The ‘const’ Keyword

You can also use the “const” keyword with the variable declaration to define a constant.

const type variable = value;

So, here’s what we do to define a constant called “RADIUS”:

const int RADIUS = 5;

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

int main()
{
const float PI = 3.142;
const int RADIUS = 5;
cout<<endl;
cout<<"Circumference of circle: "<<2*PI*RADIUS;
cout<<endl;
}``````

The word “const” is used to define two constants in this program.

We have set up constants in the main function of the code shown above. We can also define these constants globally, above all the functions, as an alternative.

In that case, these constants will affect everything everywhere, so they are called “Global constants.”

Conclusion

This is the last part of this C++ Programming tutorial on Constants. When we want to define certain values in math that don’t change, we can use constants.