ha****@alcatel-lucent.com wrote in news:1192113564.121112.142920
@k79g2000hse.googlegroups.com:
Hi Folks,
Just wanted to know few things about static variables.
why static variables cannot be accessed in other files by using extern
in c/c++?
Because that is the way they are defined. Basically, a static variable
doesn't have external linkage. That is, no symbolic information is
stored in the resultant object file to allow use by other files. This
is to keep a check on global name pollution. In C++, the preferred way
to do this is with anonymous namespaces, but C didn't/doesn't have
namespaces.
whats the difference between global and static variables?
Global variables are accessible throughout the program, static variables
are not. Also, global variables can't be defined within a function, but
can be accessed via an extern declaration scoped within a function.
Both these variables behave almost in similar manner, then where is
the memory allocated for these variables?
This would be implementation dependent, but traditionally statics and
globals are kept in separate memory spaces but neither is on the heap.
As I said before, the difference is primarily in whether the variable is
accessible outside the current translation unit (or .c/.cpp) file.
>
Appreciate your comments/suggestions.
Hope that helps and isn't too far from what the actual spec says. :)
joe