What is the method for clearing array elements in the C language?

One way to clear the elements of an array in C language is to achieve this by looping through the array and assigning each element to be 0. Here is an example code:

#include <stdio.h>

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int i;

    for (i = 0; i < 5; i++) {
        arr[i] = 0;
    }

    printf("Array elements after clearing: ");
    for (i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

In the example code above, we start by defining an array named ‘arr’ with 5 elements. Then, we use a for loop to iterate through the array and set each element to 0. Finally, we use another for loop to print out the elements of the array after they have been cleared.

bannerAds