I found that in C language but I don’t know other language it need to initialize an array to 0 or not.
for (i = 0; i < MAXSIZE; i++) {
a[i] = 0;
}
Is there a reason for doing this?
In what cases do I need to initialize an array to 0 and in what cases do I not?
The reason you do this is because the array would contain garbage values (the values previously stored at that memory location). Zeroing out is good practice because zero is a value that, when used incorrectly, causes problems that are easy to find and debug (NULL dereference and such). On the other hand, garbage values may give you the impression that your program works, only to have it crash in some unrelated part of the code because your incorrect usage of the garbage value clobbered some other memory location.