Global variables in C++

How Global variables are used in C++?

Explanation

Variables that are declared outside the scope of a function, so that it can be used anywhere in the program is known as Global Variables.

Example :



#include <iostream.h>
float pi = 3.14;
int main( )
{
int area,rad;
rad = 100;
area = pi*rad*rad;
cout << "Area of a Circle:" << area << '\n';
return 0;
}

Result:

Area of a Circle: 31400

In the above example "pi" is declared as global variable outside any function. But the "pi" value is used inside the main function.

C++ Tutorial


Ask Questions

Ask Question