C – Find Array Length

To find the length of an array in C programming, use sizeof operator. By dividing the size of total array with the size of an element in the array, we get the number of elements in the array.

Examples

In the following program, we initialize an array of size 4. We will get the size of this array programmatically using sizeof property.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int arr[] = {2, 4, 6, 8};
    int arrLength = sizeof arr / sizeof arr[0];
    printf("Array Length : %d\n", arrLength);
    return 0;
}

Output

Array Length : 4
Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned how to find the length of an array programmatically using sizeof operator.