在 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;
}