11

I know static is an overloaded keyword in C. Here, I'm only interested in its use as a keyword to enforce internal linkage.

If you have a global variable declared in a .c file, what is the difference between using static and not using static? Either way, no other .c file has access to the variable, so the variable is basically "private" to the file, with or without the static keyword.

For example, if I have a file foo.c, and I declare a global variable:

int x = 5;

That variable x is only available to code inside foo.c (unless of course I declare it in some shared header file with the extern keyword). But if I don't declare it in a header file, what would be the difference if I were to type:

static int x = 5.

Either way, it seems x has internal linkage here. So I'm confused as to the purpose of static in this regard.

Channel72
  • 145
  • 2
  • 5
  • 2
    You almost answered your own question - you said "if I don't declare it in a header file", meaning "if I don't write a declaration with the `extern` keyword". The point is that it's *impossible* to write such a declaration if you've declared your variable static, while if you haven't, someone else could come along and do it, link against your code, and have access to your variable. (As well as what Pascal Cuoq mentions.) – Cascabel Aug 19 '10 at 19:47
  • possible duplicate of [What does "static" mean in a C program?](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program). in particular, I address that case in my answer: http://stackoverflow.com/a/14339047/895245 – Ciro Santilli OurBigBook.com Jun 19 '15 at 08:22

2 Answers2

13

If you have a global variable declared in a .c file, what is the difference between using static and not using static? Either way, no other .c file has access to the variable [...]

A different file could declare x:

extern int x;

That would allow code referencing x to compile, and the linker would then happily link those references to any x it finds.

static prevents this by preventing x from being visible outside of its translation unit.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • That's interesting. Doesn't that seem like a very prescient feature for a time when data encapsulation wasn't widely practiced or encouraged? – Channel72 Aug 19 '10 at 19:51
  • In other words, global allows multiple declarations across files to share a single definition using 'extern' where as static will not. Right? – naivnomore Aug 19 '10 at 20:08
  • @Channel72: Well, that's why global data is frowned upon: it isn't encapsulated. If it were, it wouldn't be _global_ data. – sbi Aug 19 '10 at 22:25
  • @naivnomore: You do this all the time. Only you put the declarations into header files and include those from other translation units. (And you omit the `extern` for function declarationss, because they're implicitly `extern`.) For the compiler it doesn't matter (after the preprocessing) where those declarations actually came from. – sbi Aug 19 '10 at 22:29
  • @Channel72, it isn't prescient so much as making available a feature that the assembler of the day already offered. – RBerteig Aug 20 '10 at 01:29
6

There is only one "namespace", so to speak, in C. Without the "static" keyword you are not protected from another file using the name "x" (even if you do not make it visible in your own library's header).

Try to link together several C files containing a non-static variable x (interleaving read and write accesses from functions in each file), and compare with the situation where these variables are declared static.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • Of course, if none of those files *initialize* the global x, you might be getting a common block.... even if the types of your various `x` differ. Thank the need to also support FORTRAN in the linker for that one. – RBerteig Aug 20 '10 at 01:31
  • @RBerteig Yes, without initialization was what I meant. The reader wondering what we are talking about can visit e.g. http://stackoverflow.com/questions/1490693/tentative-definitions-in-c99-and-linking – Pascal Cuoq Aug 20 '10 at 08:53