Initialize a 2D array with all 0's in C | Techie Delight (2024)

C

This post will discuss how to initialize a 2D array with all 0’s in C.

1. Using Initialization Syntax

To initialize a 2D array with zeros, you can make use of the explicit initialization property of the arrays, which states that the uninitialized part of an array is initialized with static storage duration. Consider the array declaration – int array [M][N] = {1};, which sets the element at the first column in the first row to 1 and all other elements to 0.

We can use this trick to explicitly initialize only the first element of the array with 0, causing the remaining elements to be initialized with zeros automatically. This is demonstrated below for a 4 × 4 matrix.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#include <stdio.h>

// M x N matrix

#define M 4

#define N 4

int main(void)

{

int mat[M][N] = { 0 };

// print the 2D array

for (int i = 0; i < M; i++) {

for (int j = 0; j < N; j++) {

printf("%d ", mat[i][j]);

}

printf("\n");

}

return 0;

}

DownloadRun Code

Output:

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

2. Using memset() function

You can also use the memset() function to initialize array elements with 0 or -1, which overwrites the allocated memory with 0’s or 1’s.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include <stdio.h>

#include <string.h>

// 2D array dimentions

#define M 4

#define N 4

int main(void)

{

int mat[M][N];

memset(mat, 0, sizeof(mat));

// print the matrix

for (int i = 0; i < M; i++)

{

for (int j = 0; j < N; j++) {

printf("%d ", mat[i][j]);

}

printf("\n");

}

return 0;

}

DownloadRun Code

Output:

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

3. Using Designated Initializers

Finally, you can use designated initializers and name the array indices to be initialized within the initializer list. Its usage is demonstrated below, where the code explicitly initializes the first element of the array, and the remaining elements are automatically initialized with 0.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

#include <stdio.h>

// 2D array dimentions

#define M 4

#define N 4

int main(void)

{

int mat[M][N] = { [0][0]=0 };

// print the matrix

for (int i = 0; i < M; i++)

{

for (int j = 0; j < N; j++) {

printf("%d ", mat[i][j]);

}

printf("\n");

}

return 0;

}

DownloadRun Code

Output:

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

That’s all about initializing a 2D array with zeros in C.

Rate this post

Average rating 5/5. Vote count: 12

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?


Thanks for reading.

To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and support our growth. Happy coding :)


Initialize a 2D array with all 0's in C | Techie Delight (1)

Vivek Srivastava

Software Engineer | Content Writer | 12+ years experience

Initialize a 2D array with all 0's in C | Techie Delight (2024)

FAQs

How to initialize a 2D array with zeros in C? ›

Initializing two-dimensional arrays:

Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row.

How do you set all values of a 2D array to 0? ›

To clear a 2D array in C++, you can use the nested loops to iterate through each element of the array and set the values to 0 or any other default value. In the above example, we have initialized a 2D array of 3x3 and then used two nested loops to set each element to 0.

How to initialize an array to all zero in C? ›

  1. You can initialize the elements of the array to zero, at declaration.
  2. int array[ n ] = { 0 };
  3. double array[ n ] = { 0.0 };
  4. Or, you use a loop, as the other answer shows.
Feb 2, 2022

How to initialize 2D char array with null in C? ›

In C/C++, you can initialize a char array with null by assigning the null character '\0' to its first element. Here's an example: char myCharArray[100];

How to initialize a 2D array in C? ›

When declaring a 2D array, you need to specify the number of rows and columns using the following syntax: data_type array_name[row_size][column_size]; For example, the following code declares a 2D array of integers with 3 rows and 4 columns: int matrix[3][4];

How do you initialize an array with zeros? ›

If you want to create a new array with all elements initialized to zero, you can use the new operator along with the Arrays. fill method. This will create a new array with all elements set to zero, using the Java Stream API.

How do you set values in a 2D array? ›

To explicitly put a value in an array, you can use assignment statements with the name of the array followed by the row index in square brackets followed by the column index in square brackets and then an = followed by a value.

How do you declare a 2D array? ›

To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.

Do 2D arrays start at 0? ›

Just like 1D arrays, 2D arrays are indexed starting at 0 .

How to initialize array in C? ›

There are a couple of ways you can initialize an integer array in C. The first way is to initialize the array during declaration and insert the values inside a pair of opening and closing curly braces, {} . The general syntax to do that looks like this: data_type array_name[array_size] = {value1, value2, value3, ...};

How to declare and initialize an array in C? ›

To simultaneously declare and initialize an array in C, use the following syntax. // initialize an array at the time of declaration. int Demo_array[] = {100, 200, 300, 400, 500}; The array size in the above syntax has not been specified, but the compiler in C will allocate an array size of 5 integer elements.

How to initialize an empty array in C programming? ›

In C, you can create an empty array by simply declaring an array with zero elements. However, an empty array cannot be used to store any values. Here is an example of how to declare an empty array in C: int arr[0];

How to initialize char 2D array in C? ›

For example, “char a[2][5];” declares an array of two arrays of 5 char. To access the first character of the first array, use a[0][0]. To access the last,character of the last array, use a[1][4]. Note that “char a[2,5];” will compile, but will not give you a two dimensional array.

How to set a value in a 2D array C? ›

To explicitly put a value in an array, you can use assignment statements with the name of the array followed by the row index in square brackets followed by the column index in square brackets and then an = followed by a value.

How do you declare and initialize a 2 D array? ›

To declare a 2D array, specify the type of elements that will be stored in the array, then ( [][] ) to show that it is a 2D array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference.

Top Articles
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 5584

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.