VOOZH about

URL: https://www.geeksforgeeks.org/c/how-to-initialize-array-to-0-in-c/

⇱ How to Initialize Array to 0 in C? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Initialize Array to 0 in C?

Last Updated : 17 Jun, 2024

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 C

There 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.

  1. With Declaration using Initializer List
  2. After Declaration using Loops or memset()

Array Initialization with Declaration

The 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


Output
0 0 0 0 0 
0 0 0 0 0 

After Declaration Array Initialization

If 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.


Output
0 0 0 0 0 


Comment
Article Tags: