Cprogramming 简明教程

Static Variables in C

默认情况下, C variable 被归类为 auto storage type 。静态变量可用于在调用不同函数之间保留某个值。静态变量还用于存储应该在多个函数之间共享的数据。

By default, a C variable is classified as an auto storage type. A static variable is useful when you want to preserve a certain value between calls to different functions. Static variables are also used to store data that should be shared between multiple functions.

Static Variables

static variables 属于静态存储类,它们只被初始化一次并且保留值直到程序结束, static 关键字用于声明静态变量。

The static variables belong to the static storage class, they are initialized only once and preserve the values till the end of the program, The static keyword is used to declare the static variables.

Features of Static Variables

以下是 C 编程语言中静态变量的一些特性 −

The following are some of the features of static variables in C programming language −

  1. The compiler allocates space to a static variable in the computer’s main memory.

  2. Unlike auto, a static variable is initialized to zero and not garbage.

  3. A static variable is not re-initialized on every function call, if it is declared inside a function.

  4. A static variable has local scope.

Declare a Static Variable

要在 C 语言中声明一个静态变量,请使用 static 关键字并赋予初始值。以下是声明静态变量的语法:

To declare a static variable in C language, use the static keyword and assign the initial value. Following is the syntax to declare a static variable:

static datatype var = value;

Here

Here,

  1. datatype represents the type of variable like int, char, float, etc.

  2. var is the name of variable given by user.

  3. value is any value given to initialize the variable. By default, it is zero.

Examples of Static Variables in C

Example: Using Static Variable

下面是您如何在 C 语言中使用静态变量的一个示例 −

Here is an example of how you can use a static variable in C language −

#include <stdio.h>

int main(){

   auto int a = -28;
   static int b = 8;

   printf("The value of auto variable: %d\n", a);
   printf("The value of static variable b: %d\n",b);

   if(a != 0)
      printf("The sum of static variable and auto variable: %d\n",(b+a));

   return 0;
}

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

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

The value of auto variable: -28
The value of static variable b: 8
The sum of static variable and auto variable: -20

Example: Create Counter Function W/O Using Static Variable

在这个示例中, x 默认情况下是一个自动变量,并在每次调用 counter() 函数时初始化为 0。在每次后续调用中,它都会重新初始化。

In this example, x is an auto variable by default and initialized to 0 every time when the counter() function is called. On each subsequent call, it gets re-initialized.

#include <stdio.h>

int counter();

int main(){
   counter();
   counter();
   counter();
   return 0;
}

int counter(){
   int x;
   printf("Value of x as it enters the function: %d\n", x);
   x++;
   printf("Incremented value of x: %d\n", x);
}

运行代码并检查其输出:

Run the code and check its output −

Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 0
Incremented value of x: 1

然而,当 counter() 函数中的变量 x 声明为 static 时,它会在首次调用 counter() 函数时初始化为“0”。在每次后续调用中,它不会重新初始化。相反,它会保留较早的值。

However, when the variable x in the counter() function is declared as static, it is initialized to "0" when the counter() function is called for the first time. On each subsequent call, it is not re-initialized. Instead, it retains the earlier value.

Example: Create Counter Using Static Variable

将“x”的声明更改为“static int x = 0;”并再次运行该程序 −

Change the declaration of "x" to "static int x = 0;" and run the program again −

#include <stdio.h>

int counter();

int main(){
   counter();
   counter();
   counter();
   return 0;
}

int counter(){
   static int x = 0;
   printf("Value of x as it enters the function: %d\n", x);
   x++;
   printf("Incremented value of x: %d\n", x);
}

现在,当您运行这段代码时,它将生成以下输出 −

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

Value of x as it enters the function: 0
Incremented value of x: 1
Value of x as it enters the function: 1
Incremented value of x: 2
Value of x as it enters the function: 2
Incremented value of x: 3

Passing Static Variable to Function

您可以将静态变量传递给函数。但是,形式参数不能声明为静态,因为 C 将函数参数用作函数内部的局部自动变量。

You can pass a static variable to a function. However, a formal parameter cannot be declared as static, as C uses the function parameters as local auto variables inside a function.

Example

在此代码中,我们向 function 传递了一个静态变量。但是,其值的变化不会反映在调用函数中。

In this code, we pass a static variable to a function. However, the change in its value is not reflected in the calling function.

#include <stdio.h>

int myfunction(int x);

int main(){

   static int x = 5;
   myfunction(x);
   printf("in main - x:%d\n", x);

   return 0;
}

int myfunction(int x){
   x++;
   printf("Incremented value of x: %d\n", x);
}

运行该代码并检查其输出 −

Run the coce and check its output −

Incremented value of x: 6
in main - x:5

Similarities Between Static and Global Variables

静态变量与 global variable 有一些相似之处。如果两者均未显式初始化,则两者都初始化为“0”(对于数字类型)或“空指针”(对于 pointers )。

A static variable has certain similarities with a global variable. Both of them, if not explicitly initialized, both are initialized to "0" (for numeric types) or "null pointers" (for pointers).

静态变量的范围局限于在其中声明的函数或块中。这不同于全局变量,全局变量可在整个程序中访问。此外,还可以使用 extern 关键字在另一个代码文件中导入静态变量。

The scope of a static variable is restricted to the function or the block in which it is declared. This is unlike a global variable, which is accessible throughout the program. Also, a static variable can be imported in another code file, as we do by using the extern keyword.

Example

您也可以将全局变量声明为静态。请查看以下示例 −

You can declare a global variable as static too. Take a look at the following example −

#include <stdio.h>

int myfunction();

static int x = 5;

int main(){
   myfunction(x);
   printf("Inside the main function, x: %d\n", x);
   return 0;
}

int myfunction(){
   x++;
   printf("Incremented value of x: %d\n", x);
}

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

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

Incremented value of x: 6
Inside the main function, x: 6

最好使用静态变量仅在文件中访问。另一方面,使用全局(使用 extern )变量从程序中的任何位置进行访问(如果在其他文件中声明 extern )。

It is better to use static variables to be accessible only within a file. On the other hand, use global (with extern) variables to be accessible from anywhere in a program (if declared extern in other files).