To determine the size of an array in C, use the formula:
sizeof(array) / sizeof(array[0]);
sizeof(array)
gives the total memory size of the array, and sizeof(array[0])
gives the size of a single element. Dividing these provides the number of elements in the array. For example:
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printf("Array size: %d", size);
This works only for statically declared arrays, not for pointers or dynamically allocated arrays, as sizeof
on a pointer only gives the size of the pointer, not the full array.