In C, objects with automatic storage have an indeterminate value if they are not initialized, but static objects does not. From the standard:
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
- if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
We all know that C is a pretty unforgiving language that gives all the responsibility to the programmer, so it makes me wonder a bit why they decided to zero initialize static objects. I also wonder why automatic arrays are completely zero initialized if and only if at least one element is manually initialized. But what makes me most curious is why they did not choose to do this either for everything or nothing.
What is the rationale behind this?