Monday 19 December 2016

What is Constant and how to declare a constant in C++ ?


Constant:

Constant is a permanent value that cannot be changed. In C++ constant can be declared by using const keyword.

Types of constant:

1. Numeric
  • int constant
  • float constant
2. Character
  • string constant
  • character constant
const int g =9.8;

If you change the value of constant after initialization an error will be generated by the compiler cannot modify const object.

#define:

#define is also used to define a constant.

General syntax:

#define constant_name value
  Example: #define pai 3.14