What is the difference between "File scope" and "program scope"
Asked Answered
H

4

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?

Hostler answered 25/12, 2012 at 2:58 Comment(0)
C
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.

Crescentia answered 25/12, 2012 at 3:28 Comment(2)
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 en.wikipedia.org/wiki/Linkage_(software) to be equally confusing. The thing is that being ``accessible" and visible are not the same.Hirundine
(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.Hirundine
O
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).

Oleta answered 25/12, 2012 at 3:9 Comment(4)
extern int foodata_private will compile, but it won't link.Hazing
why not put the variables in the header file and use them when needed, without resorting to any extern?Kigali
@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 #3692335 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.Oleta
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.Oleta
C
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.

Crescentia answered 25/12, 2012 at 3:28 Comment(2)
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 en.wikipedia.org/wiki/Linkage_(software) to be equally confusing. The thing is that being ``accessible" and visible are not the same.Hirundine
(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.Hirundine
B
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.

Bicolor answered 25/12, 2012 at 3:6 Comment(0)
P
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.

Pantie answered 25/12, 2012 at 3:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.