Code:
extern int global;
static int fileLocal;
void foo ( int param ) {
int a;
static int b;
for ( int i = 0 ; i < param ; i++ ) {
int a;
}
a = param;
}
global is visible to everything in the current process
fileLocal is visible (by name) only to functions in the current source file.
a (the first one) is a local variable
b is local to foo only, but it's value is preserved across function calls.
i and a (the other one) have a scope limited to the for loop block itself.
static variables (in one file) can be used in other files by passing pointers around (eg. asctime())
In C++, you also have namespaces, so things like this are possible.
Code:
namespace foo {
int a;
}
namespace bar {
int a;
}
int main()
{
foo::a = 1;
bar::a = 2;
}
> I suppose its all relative to the operating system in the end
You can add to the above say
- shared memory, which persists beyond the life of a single process (until the last process sharing it exits)
- files, which persist beyond the run-time of the OS.