24

A variable declared globally is said to having program scope
A variable declared globally with static keyword is said to have file scope.

For example:

int x = 0;             // **program scope**   
static int y = 0;      // **file scope**  
static float z = 0.0;  // **file scope** 

int main()  
{  
   int i;   /* block scope */  
   /* .
      .
      .
   */ 
   return 0;  
}  

What is the difference between these two?

cpx
  • 17,009
  • 20
  • 87
  • 142
yuvanesh
  • 1,093
  • 2
  • 9
  • 19

4 Answers4

18

Variables declared as static cannot be directly accessed from other files. On the contrary, non-static ones can be accessed from other files if declared as extern in those other files.

Example:

foo.c

int foodata;
static int foodata_private;

void foo()
{
    foodata = 1;
    foodata_private = 2;
}

foo.h

void foo();

main.c

#include "foo.h"
#include <stdio.h>

int main()
{
    extern int foodata; /* OK */
    extern int foodata_private; /* error, won't compile */

    foo();

    printf("%d\n", foodata); /* OK */

    return 0;
}

Generally, one should avoid global variables. However, in real-world applications those are often useful. It is common to move the extern int foo; declarations to a shared header file (foo.h in the example).

Roman Dmitrienko
  • 3,375
  • 3
  • 37
  • 48
  • 2
    `extern int foodata_private` will *compile*, but it won't link. – bool3max Dec 21 '18 at 20:56
  • why not put the variables in the header file and use them when needed, without resorting to any `extern`? – Alexander Cska Sep 21 '19 at 12:09
  • @AlexanderCska this is not a good idea usually. If that variable is not `static`, it becomes defined in _every_ source code file that includes the header file. Then the program may or may not link, depending on whether the variable is initialized (see https://stackoverflow.com/questions/3691835/why-uninitialized-global-variable-is-weak-symbol for details). Both outcomes are almost always not what you want. If the variable _is_ `static`, it gets defined as a private symbol in every translation unit. Again, usually it's not what you want. – Roman Dmitrienko Sep 23 '19 at 13:57
  • The right way to share a global variable in C between modules is to define the variable once in a source code file, and declare it as `extern` in the corresponding header file. Then, all source code files that include this header file will properly share the variable. – Roman Dmitrienko Sep 23 '19 at 13:59
17

In C99, there's nothing called "program scope". In your example variable x has a file scope which terminates at the end of translation unit. Variables y and z which are declared static also have the file scope but with internal linkage.

C99 (6.2.2/3) If the declaration of a file scope identifier for an object or a function contains the storage class specifier static, the identifier has internal linkage

Also, the variable x has an external linkage which means the name x can is accessible to other translation units or throughout the program.

C99 (6.2.2/5) If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.

cpx
  • 17,009
  • 20
  • 87
  • 142
  • 5
    Just a comment, saying ``x has a file scope which terminates at the end of translation unit`` (which means it is visible only in this unit) and ``x can is [be] accessible to other translation unit`` is technically correct but may be a bit confusing. (Nonetheless I found the wiki page http://en.wikipedia.org/wiki/Linkage_(software) to be equally confusing. The thing is that being ``accessible" and visible are not the same. – Ying Zhang Jun 07 '14 at 18:47
  • 6
    (comment too long) After reading the C99 standard I think I sort of get it: suppose x has file scope and external linkage. If you want to have access to x in another translation unit, you will have to declare x there as well. Then since the two x's are linked, they refer to the same thing. – Ying Zhang Jun 07 '14 at 18:47
0

C programs can be written in several files, which are combined by a linker into the final execution. If your entire program is in one file, then there is no difference. But in real-world complex software which includes the use of libraries of functions in distinct files, the difference is significant.

Devin Ceartas
  • 4,743
  • 1
  • 20
  • 33
0

A variable with file scope is only visible from its declaration point to the end of the file. A file refers to the program file that contains the source code. There can be more than one program files within a large program. Variables with program scope is visible within all files (not only in the file in which it is defined), functions, and other blocks in the entire program. For further info. check this : Scope and Storage Classes in C.