静态变量仅初始化一次。编译器将变量保留到程序结束。静态变量可以在函数内部或外部定义。它们是本地的。静态变量的默认值为零。静态变量在程序执行之前一直有效。
这是C语言中的静态变量的语法,
static datatype variable_name = value;
这里,
datatype-变量的数据类型,例如int,char,float等。
variable_name-这是用户给定的变量名。
值-来初始化变量的任何值。默认情况下,它为零。
这是C语言中的静态变量示例,
#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;
}输出结果
这是输出
The value of auto variable : -28 The value of static variable b : 8 The sum of static variable and auto variable : -20