Friday 6 March 2015

Keyword Constant in C++

How to cleared constant in C++ programming Keyword Constant in C++    When we declared some constant in C++ then we cannot Change... thumbnail 1 summary

How to cleared constant in C++ programming

Keyword Constant in C++ 

 

When we declared some constant in C++ then we cannot Change its value and it is called constant. write the keyword const before  declaring canstant.
Syntax 

const int a=20;
const float pi=3.14;
Note: A constant must be initialize to some value other wise you will get error in C++.

Example 1
#include<iostream.h>
int main()
{
const int a=5;                     
cout<<a;
system("pause");
return 0;
};
output: 5

Example 2
int main()
{
const int a;
a=5;             
cout<<a;
system("pause");
return 0;
};
Output: error you are not initialize your constant
Example 3
int main()
{
const int a=5;
a=9;      
system("pause");
return 0;
};
Output:
error you cannot change the value of a because it is constant.