I have a question about the initialization of static variables in C. I know if we declare a global static variable that by default the value is 0. For example:
static int a; //although we do not initialize it, the value of a is 0
but what about the following data structure:
typedef struct
{
int a;
int b;
int c;
} Hello;
static Hello hello[3];
are all of the members in each struct of hello[0], hello[1], hello[2] initialized as 0?
Yes, all members are initialized for objects with static storage. See 6.7.8/10 in the C99 Standard (PDF document)
To initialize everything in an object, whether it’s
staticor not, to 0, I like to use the universal zero initializerThere is no partial initialization in C. An object either is fully initialized (to
0of the right kind in the absence of a different value) or not initialized at all.If you want partial initialization, you can’t initialize to begin with.