When a variable is in global scope, the whole program can use it.
When the variable is in local scope, only the scope that has the variable may use it.
In order to create a global variable, you must create the variable outside of int main().
In order to create a local variable, you must create the variable within the {} brackets of anything. But it would only exist it those brackets. Once you go to a different bracket, it will disappear.
Example: Global scope variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int number; // this is a global variable
int main()
{
number = 7; //you can use this variable even tho it wasnt created in this scope.
return 0;
}
Example: Local scope variable.
[code]
int main()
{
int number; // this variable can only be used within the int main scope.
number = 7;
return 0;
}
|
iostream is the header file that allows you to do input and output.
You would need it if you would like to do something like
std::cout<< "Hello world \n";
namespace std is a shortcut of using std without having to type std all the time.
So, when you use namespace std, all you need to type is
cout<< "Hello world \n";
instead of
std::cout<< "Hello world\n";
Okay I hope I helped. I'm new too!