Why is an external link not associated with a static variable?

Why does extern int n not compile when n is declared (in another file) static int n , but works when int n declared? (Both of these ads were in the file area.)

Basically, why does int n in the file area not match static int n in the same area? Is it only in relation to appearance? If so, what about extern am I missing?

+46
c static extern
May 15, '10 at 21:08
source share
4 answers

The whole purpose of static is to declare that a variable is closed to the source file that is declared in . Thus, he does exactly his job of preventing connections to the outside.

Keep in mind that there are four options for defining a file's scope variable:

  • int blah = 0; - blah is defined in this file and is accessible from other files. Definitions in other files are duplicates and lead to errors.
  • extern int blah; - blah must be defined elsewhere and reference this file.
  • int blah; - This is the moral equivalent of FORTRAN COMMON . You can have any number of them in files, and all of them are allowed by the linker for one common int . (*)
  • static int blah; (optional with an initializer) - this is static. It is completely closed to this file. It is not visible to external files in other files, and you may have many different files that declare static TYPE blah; and they are all different .

For purists in the audience: 'file' = compilation unit.

Note that static internal functions (not in the file area) are even more severely limited: if two functions declare static int bleh = 0; even in one file, they are not connected.

(*): for those of you who are not familiar: in a regular template, one compilation unit should define a global variable, while others may refer to it. He "lives" in this unit of compilation. In case (3) above, not a single file (or all files) defines it. If two files say int blah = 0; , the linker will complain about several definitions. If two files say int blah; , the linker cheerfully creates a single global int and makes all the code reference it.

+94
May 15 '10 at 21:10
source share

In the C standard, there are two areas for variables declared outside of a function. The static variable is visible only inside the compilation unit (i.e. the file) that declared it, and non-static variables are visible throughout the program. The extern declaration says that the location of the variable is not yet known, but will be sorted by the linker; it is compatible with non-static variables, but extern static is just crazy talk!

Of course, in practice these days there are other possibilities. In particular, there are currently levels of coverage between one source file and an entire program; the level of one shared library is useful (configured using mechanisms such as GCC function attributes). But this is just a variation on the topic of non-static variables; static retains the same interpretation as before.

+5
May 15, '10 at 21:21
source share

iv.c: 2: 1: error: several storage classes in ad specifiers extern static int i; ^

This is what we get from trying a static variable. Declaration extern static int i; - similar to the declaration float int i; You cannot have float and int in the same declaration? Similarly, you cannot have extern and static in the same declaration.

+1
Dec 17 '15 at 4:41
source share

According to the MSDN documentation:

When a variable is changed, the static keyword indicates that the variable has a static duration (it is allocated when the program starts and freed when the program ends) and initializes it to 0, unless another value is specified. When changing a variable or function in the file area, the static keyword indicates that the variable or function has an internal binding (its name is not visible from outside the file in which it is declared).

http://msdn.microsoft.com/en-us/library/s1sb61xd(v=vs.80).aspx : June 2013

0
Jun 25 '13 at 21:44
source share



All Articles