c语言中全局变量和静态变量
Prerequisite: Storage classes in C
先决条件: C语言中的存储类
Automatic (auto) and static both are the keywords which are used under the storage classes, and they define the scope, lifetime, default value and memory segment of the variables.
自动( auto )和静态都是在存储类下使用的关键字,它们定义变量的范围,生存期,默认值和内存段。
1)自动('auto')变量 (1) Automatic (‘auto’) variable)
The all local variables which are defined within the function are known as auto (automatic) variables unless not specified i.e. by default a local variable is an auto variable. There is no need to put the keyword auto (it’s an optional) while declaring a local variable.
除非未指定,否则函数中定义的所有局部变量都称为自动 (自动)变量,即默认情况下,局部变量是自动变量。 在声明局部变量时,无需放置关键字auto (它是可选的)。
An auto variable created a new each time when the function (in which variable is declared) is called and destroyed when the program’s execution leaves the function.
每次调用函数(在其中声明变量)时都会创建一个新的自动变量,并在程序执行离开该函数时销毁该变量。
Example:
例:
#include <stdio.h>
int main()
{
int a;
auto int b;
a=10; b=20;
printf("a=%d, b=%d\n",a,b);
return 0;
}
Output
输出量
a=10, b=20
Note: Here, a and b both are automatic (auto) variables.
注意:此处, a和b都是自动( auto )变量。
2)静态变量 (2) Static variables )
It is much similar to automatic variable; all properties are almost same except few. A static variable does not create a new each time and does not destroy when program’s execution leaves the function. It declares once and destroys once when program’s execution finishes.
它与自动变量非常相似。 除少数外,所有属性几乎相同。 静态变量不会每次都创建一个新变量,并且在程序执行离开该函数时也不会销毁。 它只声明一次,并在程序执行完成时销毁一次 。
Declaration Syntax:
声明语法:
static data_type variable_name = initial_value;
Declaration Example:
声明示例:
static int count =0;
Example:
例:
#include <stdio.h>
void fun(void)
{
auto int a=0;
static int b=0;
printf("a = %d, b = %d\n",a,b);
a++;
b++;
}
int main()
{
int loop;
//calling function 10 times
for(loop=0; loop<5; loop++)
fun();
return 0;
}
Output
输出量
a = 0, b = 0
a = 0, b = 1
a = 0, b = 2
a = 0, b = 3
a = 0, b = 4
In the function fun(), variable a is automatic and b is static. According to the property ‘a’ is initializing each time when function is called but b is static so it is initialized once.
在fun()函数中,变量a是自动的,而b是静态的。 根据属性,每次调用函数时a都会初始化,但是b是静态的,因此将其初始化一次。
静态和自动变量之间的差异 (Differences between static and auto variables)
Automatic (auto) variables | Static variables |
---|---|
By default all local variables are automatic variable. Keyword auto can be used to declare an automatic variable, but it is not required. | static keyword must be used to declare a static variable. |
Automatic variable’s scope is always local to that function, in which they are declared i.e. automatic variable, can be accessible within the same block. | Static variable’s scope is also local to that function in which they are declared i.e. static variable can be accessible within the same block. |
Automatic variable’s life time is local (limited), automatic variables exit till the function execution time, when program’s execution leaves the function execution, variables are destroyed. | Static variable’s life time is not limited. Since it’s scope is local but the variable will be live (exist) till the program’s execution. |
Automatic variables create a new each time when program’s execution enters in the function and destroys when leaves. | Static variable create once, when program’s execution enters in the function first time, destroys when program’s execution finishes, they do not again. |
自动(自动)变量 | 静态变量 |
---|---|
默认情况下,所有局部变量都是自动变量。 关键字auto可以用来声明一个自动变量,但这不是必需的。 | static关键字必须用于声明静态变量。 |
自动变量的作用域始终是该函数的局部变量,在该函数中声明它们,即自动变量,可以在同一块中访问。 | 静态变量的作用域对于声明它们的函数也是局部的,即静态变量可以在同一块中访问。 |
自动变量的生命周期是局部的(有限的),自动变量会一直存在,直到函数执行时间为止,当程序的执行离开函数执行时,变量将被销毁。 | 静态变量的寿命不受限制。 因为它的作用域是局部的,但是该变量将一直存在(存在),直到程序执行为止。 |
每次程序执行进入函数时,自动变量都会创建一个新变量,离开时会破坏该变量。 | 静态变量创建一次,当程序的执行第一次进入函数时,在程序执行完成时销毁,它们不再。 |
翻译自: https://www.includehelp.com/c/difference-between-automatic-auto-and-static-variables.aspx
c语言中全局变量和静态变量