“文件范围”和“程序范围”的区别是什么

时间:2022-06-01 12:38:06

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?

这两者之间有什么区别?

4 个解决方案

#1


11  

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中,没有所谓的“程序范围”。在示例变量x中有一个文件范围,该范围终止于翻译单元的末尾。声明为静态的变量y和z也具有文件范围,但是具有内部链接。

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.

另外,变量x有一个外部链接,这意味着其他翻译单元或整个程序可以访问x的名称。

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.

C99(6.2 /5)如果一个对象的标识符声明有文件范围,没有存储类说明符,那么它的链接是外部的。

#2


14  

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.

声明为静态的变量不能直接从其他文件访问。相反,如果在其他文件中声明为extern,则可以从其他文件中访问非静态文件。

Example:

例子:

foo.c

foo.c

int foodata;
static int foodata_private;

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

foo.h

foo。

void foo();

main.c

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).

一般来说,应该避免全局变量。然而,在真实的应用程序中,这些通常是有用的。移动外部int foo是很常见的;对共享头文件的声明(foo。h的例子)。

#3


1  

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.

C程序可以在几个文件中编写,这些文件由链接器组合到最终执行。如果您的整个程序在一个文件中,那么没有区别。但是在实际的复杂软件中,包括在不同的文件中使用函数库,差异是显著的。

#4


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.

具有文件范围的变量只能从其声明点到文件末尾可见。文件是指包含源代码的程序文件。在一个大的程序中可以有多个程序文件。具有程序作用域的变量在整个程序的所有文件(不仅在定义程序的文件中)、函数和其他块中都是可见的。为进一步的信息。检查这个:C中的范围和存储类。

#1


11  

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中,没有所谓的“程序范围”。在示例变量x中有一个文件范围,该范围终止于翻译单元的末尾。声明为静态的变量y和z也具有文件范围,但是具有内部链接。

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.

另外,变量x有一个外部链接,这意味着其他翻译单元或整个程序可以访问x的名称。

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.

C99(6.2 /5)如果一个对象的标识符声明有文件范围,没有存储类说明符,那么它的链接是外部的。

#2


14  

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.

声明为静态的变量不能直接从其他文件访问。相反,如果在其他文件中声明为extern,则可以从其他文件中访问非静态文件。

Example:

例子:

foo.c

foo.c

int foodata;
static int foodata_private;

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

foo.h

foo。

void foo();

main.c

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).

一般来说,应该避免全局变量。然而,在真实的应用程序中,这些通常是有用的。移动外部int foo是很常见的;对共享头文件的声明(foo。h的例子)。

#3


1  

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.

C程序可以在几个文件中编写,这些文件由链接器组合到最终执行。如果您的整个程序在一个文件中,那么没有区别。但是在实际的复杂软件中,包括在不同的文件中使用函数库,差异是显著的。

#4


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.

具有文件范围的变量只能从其声明点到文件末尾可见。文件是指包含源代码的程序文件。在一个大的程序中可以有多个程序文件。具有程序作用域的变量在整个程序的所有文件(不仅在定义程序的文件中)、函数和其他块中都是可见的。为进一步的信息。检查这个:C中的范围和存储类。