How To Find Length Of An Array In C?

Use `sizeof(array) / sizeof(array[0])` for a statically allocated array in the same scope

Example: `int arr[] = {1, 2, 3}; size_t len = sizeof(arr) / sizeof(arr[0]);`

Use `sizeof(array) / sizeof(*array)` as an equivalent form

Use `strlen()` for C strings, not for general arrays

Example: `char str[] = “hello”; size_t len = strlen(str);`

For dynamically allocated arrays, store the length separately

Pass the array length as a function argument when needed

Use `sizeof` on pointers only gives pointer size, not array length

`sizeof` cannot determine the length of an array after it decays to a pointer in a function parameter

Suggested for You

Trending Today