94

If I have a C file like below, what is the difference between i and j?

#include <stdio.h>
#include <stdlib.h>

static int i;
int j;

int main ()
{
    //Some implementation
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 1
    Read answers to this question: http://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage-in-c – avakar Feb 16 '10 at 09:56
  • [Difference between static, auto, global and local variable in the context of c and c++](https://stackoverflow.com/questions/13415321/difference-between-static-auto-global-and-local-variable-in-the-context-of-c-a) – phuclv Jul 06 '17 at 16:38

5 Answers5

94

i has internal linkage so you can't use the name i in other source files (strictly translation units) to refer to the same object.

j has external linkage so you can use j to refer to this object if you declare it extern in another translation unit.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 2
    there shouldn't be a runtime difference if you are doing the same thing, as the compiler might generate the same code. Compilation might be slightly longer with a global though, since it has a wider scope. – Florian Castellane Oct 25 '16 at 08:14
  • Is it possible to programmatically determine the linkage of any arbitrary variable at compile time (potentially using compiler's extension) or at run time? See: https://stackoverflow.com/q/65520719/9881330. – pmor Jan 07 '21 at 21:27
  • Why do I need to use "extern" to declare "j"? Does this mean that "int j;" is a defintion and not a declaration? – Abdel Aleem Aug 05 '21 at 13:14
38

i is not visible outside the module; j is globally accessible.

That is, another module, which is linked to it, can do

extern int j;

and then be able to read and write the value in j. The same other module cannot access i, but could declare its own instance of it, even a global one—which is not visible to the first module.

nbro
  • 15,395
  • 32
  • 113
  • 196
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • 1
    Is the 'extern' declaration necessary? – Thomas Matthews Feb 16 '10 at 18:32
  • 1
    It depends on the implementation. Use of `extern` is guaranteed to not cause trouble, provided one module has the symbol as non-`extern` and public (which allocates it). Early Unix implementations merged symbols with the same name—much like a Fortran common—so `extern` was not required. – wallyk Feb 16 '10 at 19:21
  • 1
    Note that "Not visible" doesn't mean its out of scope. That means that if you would declare an i in a different compilation unit and you would remove the static i from your .c file the global i would be visible to your .c file. In contrast, if the global i were out of scope it wouldn't matter if you would have a static i declared in your .c file. The global one would never be visible. static i taking precedence over a global i defined in another compilation unit is called 'shadowing'. It also happens with function locals and is not a c++/c-only thing. – Jupiter Sep 04 '13 at 14:23
7

The difference is that i has internal linkage, and j has external linkage. This means you can access j from other files that you link with, whereas i is only available in the file where it is declared.

Hans W
  • 3,851
  • 1
  • 22
  • 21
5

i will have static linkage, i.e., the variable is accessible in the current file only.

j should be defined as extern, that is

extern int j;

in another header file (.h), and then it will have external linkage, and can be accessed across files.

nbro
  • 15,395
  • 32
  • 113
  • 196
Ramakrishna
  • 500
  • 1
  • 4
  • 11
4

Scope of static variable/function is within the same file despite you include the file as part of a different source file.

Scope of global variable is throughout the files in which it is included. To include the variable in a different source file, we use extern before the variable declaration. No memory is allocated again for the variable in this case.

extern is used to declare a C variable without defining it. extern keyword extends the visibility of the C variables and C functions. Since functions are visible through out the program by default, the use of extern is not needed in function declaration/definition. Its use is redundant.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Sravya
  • 661
  • 8
  • 9