int arr[5] = {1, 2}; for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); }
In C, when you initialize an array with fewer elements than its size, the remaining elements are set to zero.
So arr[0] = 1, arr[1] = 2, and arr[2], arr[3], arr[4] = 0.
int arr[3] = {5, 10, 15}; for (int i = 0; i < 3; i++) { printf("%d ", arr[i]); }
The array has size 3 and all elements are initialized explicitly.
So the output prints 5, 10, and 15 in order.
int arr[] = {7, 8, 9, 10}; for (int i = 0; i < 4; i++) { printf("%d ", arr[i]); }
When size is omitted, the compiler sets the array size to the number of initializer elements.
So arr has size 4 with values 7,8,9,10.
int arr[2][3] = {{1, 2, 3}, {4}}; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", arr[i][j]); } }
The first row is fully initialized: 1, 2, 3.
The second row has only the first element initialized to 4; the rest are zero.
int arr[] = {3, 6, 9};What is the size of the array
arr in memory?The compiler counts the number of elements in the initializer list and sets the array size accordingly.
Here, 3 elements means size 3.
