Cprogramming 简明教程

Global Variables in C

Global variables 在函数外部定义,通常位于程序顶部。全局变量在其程序的整个生命周期内持有其值,并且可以在为该程序定义的任何函数里面被访问。

Global variables are defined outside a function, usually at the top of a program. Global variables hold their values throughout the lifetime of a program and they can be accessed inside any of the functions defined for the program.

如果一个函数访问和修改一个全局变量的值,那么更新的值将可以用于其他函数调用。

If a function accesses and modifies the value of a global variable, then the updated value is available for other function calls.

如果一个变量在某个文件中定义,那么你仍然可以使用 extern 关键字将其作为全局变量访问到其他代码模块内部。extern 关键字也可用于访问全局变量而不是同名的局部变量。

If a variable defined in a certain file, then you can still access it inside another code module as a global variable by using the extern keyword. The extern keyword can also be used to access a global variable instead of a local variable of the same name.

Declaring Global Variable

C 中全局变量的声明类似于普通(局部) variables 的声明,但全局变量是在 functions 外部声明的。

The declaration of a global variable in C is similar to the declaration of the normal (local) variables but global variables are declared outside of the functions.

考虑以下语法来声明全局变量:

Consider the following syntax to declare a global variable:

data_type variable_name;

// main or any function
int main()
{
}

Example of Global Variable in C

以下程序显示了如何在程序中使用全局变量:

The following program shows how global variables are used in a program:

#include <stdio.h>

/* global variable declaration */
int g = 10;

int main(){

   /* local variable declaration */
   int a;

   /* actual initialization */
   a = g * 2;
   printf("Value of a = %d, and g = %d\n", a, g);

   return 0;
}

Output

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Value of a = 20, and g = 10

Accessing Global Variables

全局变量可以在 C 程序中的所有函数中访问。如果任何函数更新全局变量的值,则其更新值随后将可用于所有其他函数。

Global variables are accessible in all the functions in a C program. If any function updates the value of a global variable, then its updated value will subsequently be available for all the other functions.

Example of Accessing Global Variables

以下示例演示了在 C 语言中访问全局变量的示例:

The following example demonstrates the example of accessing global variables in C language:

#include <stdio.h>

/* global variable declaration */
int g = 10;

int function1();
int function2();

int main(){

   printf("Value of Global variable g = %d\n", g);

   function1();
   printf("Updated value of Global variable g = %d\n", g);

   function2();
   printf("Updated value of Global variable g = %d\n", g);

   return 0;
}

int function1(){
   g = g + 10;
   printf("New value of g in function1(): %d\n", g);
   return 0;
}

int function2(){
   printf("The value of g in function2(): %d\n", g);
   g = g + 10;
   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Value of Global variable g = 10
New value of g in function1(): 20
Updated value of Global variable g = 20
The value of g in function2(): 20
Updated value of Global variable g = 30

Scope and Accessibility of Global Variables

只有在声明之后定义的函数才能使用全局变量。全局变量在任何函数外部声明,因此默认情况下,可在同一文件中的所有函数中访问。

Global variables are available to only those functions which are defined after their declaration. Global variables are declared outside of any function, so they can be accessed by all functions within the same file by default.

Example

在此示例中,我们在 main() function 之前声明了全局变量( x )。另一个全局变量 y 是在 main() 函数之后但在 function1() 之前声明的。在这种情况下,变量 y ,即使它是全局变量,也无法在 main() 函数中使用,因为它是之后声明的。因此,您将收到错误。

In this example, we declared a global variable (x) before the main() function. There is another global variable y that is declared after the main() function but before function1(). In such a case, the variable y, even thought it is a global variable, is not available for use in the main() function, as it is declared afterwards. As a result, you get an error.

#include <stdio.h>

/* global variable declaration */
int x = 10;

int function1();

int main(){

   printf("value of Global variable x= %d y=%d\n", x, y);
   function1();
   return 0;
}

int y = 20;

int function1(){
   printf ("Value of Global variable x = %d y = %d\n", x, y);
}

当您运行这段代码时,它会产生错误 -

When you run this code, it will produce an error −

Line no 11: error: 'y' undeclared (first use in this function)
   11 | printf ("Value of Global variable x = %d y = %d\n", x, y);
      |                                                     ^

Accessing Global Variables With extern Keyword

如果想访问全局变量时,程序中也有同名局部变量,那么您应使用 extern 关键字。

If you want to access a global variable when a local variable with same name is also there in the program, then you should use the extern keyword.

Example

在此 C 程序中,我们有一个全局变量和一个同名局部变量 ( x )。现在让我们看看如何使用关键字“extern”来避免混淆 -

In this C Program, we have a global variable and a local variable with the same name (x). Now, let’s see how we can use the keyword "extern" to avoid confusion −

#include <stdio.h>

// Global variable x
int x = 50;

int main(){

   // Local variable x
   int x = 10;{
      extern int x;
      printf("Value of global x is %d\n", x);
   }

   printf("Value of local x is %d\n", x);

   return 0;
}

Output

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Value of global x is 50
Value of local x is 10

Avoid Using Global Variables

全局变量可以简化编程逻辑。可以在函数中访问全局变量,您无需使用参数传递技术将变量从一个函数传递到另一个函数。然而,在 C 程序中有太多全局变量既不明智也不高效,因为这些变量占用的内存直到程序结束时才会释放。

Global variables can simplify the programming logic. Global variables can be accessed across functions and you need not use a parameter passing technique to pass variables from one function to another. However, it is not wise or efficient to have too many global variables in a C program, as the memory occupied by these variables is not released till the end of the program.

使用全局声明不被认为是良好的编程实践,因为它不实现结构化方法。出于安全性的考虑,也不建议使用全局声明,因为它们对所有函数都是可访问的。最后,使用全局声明可能会让程序难以调试、维护和扩展。

Using global declaration is not considered a good programming practice because it doesn’t implement structured approach. Global declaration is also not advised from security point of view, as they are accessible to all the functions. Finally, using global declarations can make a program difficult to debug, maintain, and scale up.