How to define constants in C++?



You can define constants in C++ by adding the const qualifier before the declaration of the variable. 

Example

#include<iostream>
using namespace std;

int main() {
   const int x = 9;
   x = 0;
   return 0;
}

This will define the constant variable x. But it will throw an error as we are trying to rewrite the value of a constant.


Advertisements