Constants in c++


  • C++ supports named constants. Constants are declared like variables with the addition of the const keyword.
  • The const keyword is used to create a “read only” object. As an object of this type is constant, it cannot be modified at a later stage and must be initialized during its definition.
  • const double pi = 3.1415947;
  • Thus the value of pi cannot be modified by the program.
  • Once declared and initialized, a constant can be used like a variable in all but one way—a constant may not be reassigned.
  • It is illegal for a constant to appear on the left side of the assignment operator (=) outside its declaration statement.
  • The Constants in C++ are applicable to the values, which do not change during the execution of a program.
  • C++ has strict rules for variable names. A variable name is one example of an identifier.
  • An identifier is a word used to name things. One of the things an identifier can name is a variable.