C – Iterate over Array using For Loop

To iterate over Array using For Loop, start with index=0, and increment the index until the end of array, and during each iteration inside For Loop, access the element using index.

Refer C For Loop tutorial.

Examples

In the following example, we take an integer array arr, and loop over the elements of this array using For loop.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int arr[] = {2, 4, 6, 8};
    int arrLen = sizeof arr / sizeof arr[0];
    
    for (int i = 0; i < arrLen; i++) {
        printf("%d\n", arr[i]);
    }
    
    return 0;
}

Output

2
4
6
8
Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned how to iterate over array using For loop.