How to Initialize Array to 0 in C? Last Updated : 17 Jun, 2024 Comments Improve Suggest changes 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 Create Quiz Comment A akshitsaxenaa09 Follow 0 Improve A akshitsaxenaa09 Follow 0 Improve Article Tags : C Programs C Language c-array C Examples Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C5 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like