C Program to Initialize Array to 0
// C program to initialize an array to zero at declaration
#include <stdio.h>
int main()
{
// Using an initializer list
int arr1[5] = { 0 };
// Setting the first element to zero
int arr2[5] = {
0
}; // All elements are implicitly initialized to zero
// Print the arrays
for (int i = 0; i < 5; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
for (int i = 0; i < 5; i++) {
printf("%d ", arr2[i]);
}
printf("\n");
return 0;
}
Output
0 0 0 0 0 0 0 0 0 0
How to Initialize Array to 0 in C?
Initializing an array to zero is a common practice in programming to ensure that all elements start with a known value. In C, there are several ways to initialize an array to zero. In this article, we will explore different methods to do so.