C++ Constant
Types And Uses
What is Constant ?
- As similar to variable constant are data storage location.
- As the name implies constant's value do not change.
- They remain constant throughout the program.
- Unlike variable whose value can be changed anywhere in the program.
There are two types of constant in C++. They are as follows :
1) Literal Constant
float PI=3.14;
The value that is directly typed into the program is called literal constant.
Here 3.14 is called literal constant. You cannot assign a value to 3.14.
2) Symbolic Constant
Symbolic Constant are represented by name.
There are two ways to declare a symbolic constant. They are as follows :
1) By using processor directive #define.
This is old way of declaring constant. It has now became obsolete way.
This is old way of declaring constant. It has now became obsolete way.
2) By using keyword const.
This way is appropriate way to declare constant.
This way is appropriate way to declare constant.
Area Of Circle Program
#include <iostream>
#define PI 3.14
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int r;
cout << "Find the area of circle." << endl;
cout << "Enter radius : ";
cin >> r;
float area = PI * r * r;
cout << "The area of circle of radius " << r << " is "
<< area << endl;
return 0;
}
#define PI 3.14
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int r;
cout << "Find the area of circle." << endl;
cout << "Enter radius : ";
cin >> r;
float area = PI * r * r;
cout << "The area of circle of radius " << r << " is "
<< area << endl;
return 0;
}
No comments:
Post a Comment