+ 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.

20th Oct 2016, 5:22 AM
Rebeka Asryan
Rebeka Asryan - avatar
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
20th Oct 2016, 6:13 AM
Davillas
Davillas - avatar
+ 6
yes, That is called global variable
8th Nov 2016, 2:28 AM
Boorsu Gangadharam
Boorsu Gangadharam - avatar
+ 2
Yes. This is a global variable.
22nd Oct 2016, 7:52 PM
Gheorghe Cătălin Crişan
Gheorghe Cătălin Crişan - avatar
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
30th Dec 2016, 11:00 AM
Ashwak
Ashwak - avatar