To initialize an int array with all zeros, do I need to use:
int foo[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Or, will this work:
int foo[10] = {0};
To initialize an int array with all zeros, do I need to use:
int foo[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Or, will this work:
int foo[10] = {0};
int foo[10] = {0};
This is very fine :)
Note that if you do the following:
int foo[10] = {1};
Only the first element of the array will be initialized with the non-zero number whereas the rest will be initialized with zeros.
In C/C++ if you initialize just the first element of an array of known size with a value, the remainder will be zero-filled, so:
int foo[10] = {0};
will do exactly what you want.
This also works for structs:
struct bar {
int x;
int y;
char c;
} myBar = {0};
will initialize all members to 0.
The standard (C99 - 6.7.8/12 - Initialization) says this:
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
In C the grammar requires that there be at least one 'assignment-expression' inside the braces. An 'assignment-expression' can be many things from a constant or identifier up through much more complex expressions. However, an empty string doesn't qualify as an 'assignment-expression', so there has to be something between the braces.
In C++, the grammar specifically allows the '{ }' initializer, so the following would also zero-initialize the array:
int foo[10] = {};
It's probably also worth noting that in C++ the entries that don't have a specific initializer value in the initialize list will be 'value-initialized' or 'default-initialized' which might be different than being zero-initialized depending on what the constructors for the variable type are and whether the compiler is following the C++98 standrad or the C++03 standard (this is probably the only difference of any significance between C++98 and C++03). The whole situation with value vs. default initialization is rather complicated, so if you're interested see this answer: http://stackoverflow.com/questions/620137/syntax-of-new/620402#620402.
Fortunately, the difference doesn't seem to cause much trouble in practice, although if you run into it, it would proabbly cause some head scratching for a while when trying to figure out what the behavior really should be. I usually don't really think much about it - it makes my head hurt.
all elements not mentioned in the initializer will be initializes to that types zero value where applicable.
So int foo[10] = {0}; is fine, the remaining elements not mentioned will also be 0
Wow, C kind of seems to be simple, but even after years of citing the specification it's amazing how something new can still turn up.
I just looked it up in the first edition spec (ANSI/ISO 9899-1990) and, sure enough, the remainder of an auto aggregate is specified (6.5.7) "If there are fewer ... initialized implicitly...".
So: Anything non-auto. Always 0 (or, as initialized) whether initialized or not. Auto: completely initialized if you initialize any elements at all, otherwise, not initialized.