3 ms·
The intention is to default-initialize values to things that are invalid. There's no obvious "invalid" value for a plain integer, so they get zero. But char get
by kgm 11y ago
The intention is to default-initialize values to things that are invalid. There's no obvious "invalid" value for a plain integer, so they get zero. But char gets 0xFF, because a char is defined to hold UTF-8 code units, and 0xFF is not a valid UTF-8 code unit. Similar logic extends to the larger character types, and to using NaN in a float.
The idea is that you should be initializing values explicitly before using them, and default-initializing to values which will rapidly fail makes it easier to debug use-before-initialization bugs than (for example) it is in C, where values are not initialized at all.
If you explicitly want to disable default initialization, you can use void as an initializer, as in:
int x = void;