How to Initialize Array to 0 in C? Last Updated : 17 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Initialize Array to 0 in CThere are mainly two ways to initialize array in C and we can initialize arrays to zero both at the time of declaration and after declaration. With Declaration using Initializer ListAfter Declaration using Loops or memset()Array Initialization with DeclarationThe simplest way to initialize an array to zero is at the time of declaration by using an initializer list or by setting the first element to zero. C Program to Initialize Array to 0 C // 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; } Output0 0 0 0 0 0 0 0 0 0 After Declaration Array InitializationIf you need to initialize an array to zero after it has been declared, you can use a loop or the memset() function from the C standard library. C // C program to initialize an array to zero after // declaration #include <stdio.h> #include <string.h> int main() { int arr[5]; // Using a loop for (int i = 0; i < 5; i++) { arr[i] = 0; } // Using memset() memset(arr, 0, sizeof(arr)); // Print the array for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } Output0 0 0 0 0 Comment More infoAdvertise with us Next Article How to Initialize Array of Structs in C? A akshitsaxenaa09 Follow Improve Article Tags : C Programs C Language c-array C Examples Similar Reads How to Initialize a 2D Array in C? In C, a 2D Array is a type of multidimensional array in which data is stored in tabular form (rows and columns). It has two dimensions so it can store the data and can expand in two directions. In this article, we will learn how to initialize a 2D array in C.There are three main ways to initialize t 4 min read How to Initialize a 3D Array in C? In C, a 3D array is a type of multidimensional array that stores data in a three-dimensional grid. It has three dimensions, allowing it to store data in three directions: rows, columns, and depth. In this article, we will learn how to initialize a 3D array in C.There are three ways in which a 3D arr 4 min read How to Initialize Array of Structs in C? In C, arrays are data structures that store the data in contiguous memory locations. While structs are used to create user-defined data types. In this article, we will learn how to initialize an array of structs in C. Initializing Array of Structures in CWe can initialize the array of structures usi 2 min read How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C.Initializing a Dynamic Arrays 2 min read How to Initialize Array of Pointers in C? Arrays are collections of similar data elements that are stored in contiguous memory locations. On the other hand, pointers are variables that store the memory address of another variable. In this article, we will learn how to initialize an array of pointers in C. Initialize Array of Pointers in CWe 2 min read How to Initialize Char Array in Struct in C? In C++, we can also define a character array as a member of a structure for storing strings. In this article, we will discuss how to initialize a char array in a struct in C. Initialization of Char Array in Struct in CWhen we create a structure instance (variable) in C that includes a char array, we 2 min read Like