C Array Traversal: Loop Through Elements

In C language, one common method of iterating through all elements in an array is by using a loop structure.

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]); // 计算数组的长度

    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

In the above example, an integer array arr is first defined, followed by calculating the length of arr to determine the size of the array. Then, a for loop is used to iterate through all the elements in the array, and the elements are printed using the printf function.

bannerAds