asked 149k views
1 vote
How can an array be initialized with 0 in C?

1) By using a for loop to assign 0 to each element of the array
2) By using the memset() function to set all elements of the array to 0
3) By declaring the array with an initial value of 0
4) By using the calloc() function to allocate memory for the array and initialize it with 0

asked
User Taar
by
8.4k points

1 Answer

2 votes

Final answer:

In C, an array can be initialized with 0 using the memset() function, declaring the array with an initial value of 0, or using the calloc() function.

Step-by-step explanation:

The correct options for initializing an array with 0 in C are:

  1. By using the memset() function to set all elements of the array to 0
  2. By declaring the array with an initial value of 0
  3. By using the calloc() function to allocate memory for the array and initialize it with 0

For example:

int arr1[10]; memset(arr1, 0, sizeof(arr1)); int arr2[10] = {0}; int* arr3 = calloc(10, sizeof(int));

answered
User Aparna
by
8.2k points
Welcome to Qamnty — a place to ask, share, and grow together. Join our community and get real answers from real people.