What is constant in c++ describe with example?

Constants

Any value that declared as a constant means it can’t be modified during the program. It has same value through the program. It just like variable except that their value can’t be changed.

There are 2 ways to creating a constant in C++:

  • Using the qualifier constant
  • Defining a set of integer constants using enum
  1. Using the qualifier constant:-

Here are syntax of which constant is declaring in c++.

                     Syntax:

const int a=10;

             Note: If we use the const alone, if defaults to int.

                    For Example:

const a =10;

Means

const int a=10;

2. Defining a set of integer constants using enum keyword:-

Here are syntax of which constant is declaring using enum in c++.

                       Syntax:

enum {x,y,z};

This defines x,y,z as integer constants with values 0,1, and 2 respectively.

This is equivalent to

const x =0;

const y=1;

const z =2;

we can also assign values to x,y,z explicitly.

                      Example:

enum {x=100,y=200,z=50};

 

Leave a Reply