What happens inside a function if a static local variable has the same name as a global variable?
How does a static local variable differ from a global variable?
I know static local variables are not destroyed when a function returns but I
don't understand the difference between global variables though.
Static local win, but even its not destroyed when function end the only one that can access static local is the function itself, unlike global that can be accsess by every function in program
#include <iostream>
int a = 1550;
void myFunc();
int main()
{
int a = 5;
// use ::a to reference the global a
std::cout << "In main:\t" << a << "\t" << ::a << "\n";
myFunc();
{
int a = 455;
std::cout << "In local block:\t" << a << "\t" << ::a << "\n";
}
myFunc();
return 0;
}
void myFunc()
{
staticint a = 15;
std::cout << "In myFunc:\t" << a << "\t" << ::a << "\n";
}
In main: 5 1550
In myFunc: 15 1550
In local block: 455 1550
In myFunc: 15 1550