you're welcome. and dont worry about asking, that is what this forum is for :D
to your first question:
no.
1 2 3 4 5 6 7 8 9
|
int main() {
int i = 0;
if(i==0) {
//x is local in here
int x=3;
}
//here x is unknown, because it just exists locally in the {} of if
return 0;
}
|
see, a variable is only "visible" between the "nearest" { and } it is declared in. (except for global variables)
the main function is nothing special. a variable declared there is not "mightier" then any other local variable in another function.
main is just the starting point of your program. variables tehre are only known in main, not in other functions (thats why we have call by reference and call by value ;-) )
(i hope you understand what i am trying to say, my english isnt very good if i have to explain things :D )
now to the second... thats kinda right. a global and a global static variable are the same, there is no difference.
but the difference can be seen here
1 2 3 4 5
|
void something() {
static int counter = 0;
cout << "The function has been called " << counter++ << " times.\n";
return;
}
|
here you just can access the variable counter in the function, not in any other place of you code. but unlike a "normal" variable counter will remain even if the function returned. you could use a global variable for that too, but there is the problem, that it can be modified anywhere in the code. that is not safe. in the above example it would hurt if counter was global and somewhere changed. but i think you understand what i mean.