+ 7
Can we declare a variable out of the main() and out of any function?
I mean right after incuding <iostream>, using namespace std and befor main() and then use it in main or in any other function.
4 ответов
+ 9
yes you can, in case of outside declaration the variable will be accessible from all other functions, in case of inside declaration it will be accessible only in particular function
+ 6
yes, That is called global variable
+ 2
Yes. This is a global variable.
0
// C++
#include <iostream>
using namespace std;
// Global variable declaration:
int g = 20;
int main () {
cout << "Global var = " << g << " \n";
// Local variable declaration:
int g = 10;
cout << "Local var = " << g;
return 0;
}
output :
Global var = 20
Local var = 10