Hacker News new | ask | show | jobs
by lqet 2144 days ago
AFAIK only external and static variables are default initialized in C. For all other variables, the default value is undefined, so 0 is as good a choice as any other here.
4 comments

Except that uninitialised memory is substantially more likely to be 0 than any other value.
Except sometimes it is not and forgetting to initialize a variable in C/C++ leads to very insidious bugs that no one can reliably reproduce.
That's not quite true. While it is undefined 0 is a fairly common value for memory and registers meaning that your "undefined" values is likely 0 a higher than average amount of the time.
There is also the issue that (at least on some platforms) ECC memory must be initialized before being read, or an exception will occur.
if you use malloc, yes. calloc will initialize the variables