C语言中静态变量的默认值是什么?

2021年3月28日14:04:46 发表评论 742 次浏览

在C中, 如果未明确初始化具有静态存储持续时间的对象, 则:

—如果具有指针类型, 则将其初始化为NULL指针;

—如果具有算术类型, 则将其初始化为(正数或无符号)零;

—如果是集合, 则根据这些规则(递归)初始化每个成员;

—如果是联合, 则根据这些规则(递归)初始化第一个命名成员。

例如, 以下程序打印:

Value of g = 0

Value of sg = 0

Value of s = 0

#include<stdio.h>
int g;  //g = 0, global objects have static storage duration
static int gs; //gs = 0, global static objects have static storage duration
int main()
{
   static int s; //s = 0, static objects have static storage duration
   printf ( "Value of g = %d" , g);
   printf ( "\nValue of gs = %d" , gs);
   printf ( "\nValue of s = %d" , s);
  
   getchar ();
   return 0;
}

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

参考文献:

C99标准

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: