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 the standard says so. Basically, it says that if a
variable is declared static, it has internal linkage, i.e. the
use of that name in another translation unit refers to another
entity.
whats the difference between global and static variables?
There's no such thing as global variables
.
Seriously, there are at least three distinct concepts which seem
to be involved here:
-- The keyword static. Depending on how and where it is used,
this affects either the linkage, the lifetime, or both.
(The usual use of the word "static", other than as a
keyword, would refer to lifetime, and not linkage.) Thus,
for example, the effect of declaring a variable with local
scope static is different than the effect of declaring one
with namespace scope static.
-- The lifetime. C++ distinguished several different lifetimes
of variables: the three principal ones are static, auto and
dynamic (but temporary objects and exceptions have other
lifetimes). Roughly speaking, a static object lives for the
lifetime of the program (with some exceptions---but in no
case will it's lifetime end before the function exit() is
called). An auto object ceases to exist when the program
leaves the block in which it was declared, and the lifetime
of dynamic objects is entirely in the hands of the
programmer.
-- The linkage. This basically concerns whether two uses of
the same identifier refer to the same entity or not, and is
a characteristic of the identifier, not of the entity to
which it refers. (Also, things like type names or enum
constants have linkage, although you can't really speak of
their lifetime.) In C++, an identifier has "external
linkage" (all identical identifiers with external linkage
refer to the same entity, even accross translation unit
boundaries), "internal linkage" (identical identifiers with
internal linkage refer to the same entity within a single
translation unit, but refer to different entities when they
appear in different translation units), or "no linkage"
(each occurance of the identifier refers to a separate
entity). (In the above, "identifier" is always the fully
qualified identifier---two identical symbols declared in
different scopes are different identifiers.)
So before you can compare static with something else, you have
to specify what you mean by it---if you mean the keyword, you
have to specify where it occurs, because its effect varies: on a
variable at namespace scope, it changes the linkage from
exteral to internal, whereas on a local variable, it changes the
lifetime from auto to static. (All variables declared at
namespace scope have static lifetime.)
Also, note that the use of static on entities at namespace scope
has been deprecated in C++; it is considered preferable to use
an anonymous namespace.
Both these variables behave almost in similar manner, then where is
the memory allocated for these variables?
Where ever the compiler wants. If you're talking about
variables declared static at namespace scope, all it means is
that the same identifier refers to two different entities, so
the compiler will allocate the memory for two different
variables, in exactly the same way as it would if they had
different names.