Partial initialization and default zeros
What you'll learn: How C automatically fills in the blanks with zeros when you only provide some initial values for an array.
The "Shortcut" Rule
When you create an array in C, you usually list the values inside curly braces, like {1, 2, 3}. But what happens if you tell the computer, "I have room for ten items," but you only give it two?
In C, if you provide at least one value during initialization, the compiler steps in to help. It assumes that if you didn't specify the rest of the values, you wanted them to be zero. Think of it like booking a ten-seat table at a restaurant but only giving two names; the restaurant doesn't leave the other seats "random"—they simply mark them as empty (zero).
Seeing it in Action
This is a powerful shortcut. Instead of writing out a long list of zeros, you can let the compiler do the tedious work for you.
#include <stdio.h>
int main() {
// We created space for 5 integers, but only provided 2.
int scores[5] = {85, 92};
// scores[0] is 85
// scores[1] is 92
// scores[2], scores[3], and scores[4] are all automatically 0!
printf("Third element: %d\n", scores[2]);
printf("Fifth element: %d\n", scores[4]);
// The "Universal Zero" shortcut:
int allZeros[100] = {0};
// Every single one of the 100 elements is now 0.
return 0;
}
Why This Matters
If you declare an array without any curly braces at all (e.g., int scores[5];), the memory is left "dirty." It contains whatever random data was left there by the last program that used it. This is dangerous because your program might behave unpredictably.
By using partial initialization, even just {0}, you ensure your array starts with a clean slate. It is a safety net that prevents "garbage values" from ruining your calculations. Note that this only works at the moment you create the array; you cannot use the curly brace shortcut to reset an array back to zero later on in your code.
Key takeaway
If you initialize an array with at least one value, C automatically fills all remaining unassigned slots with zero.