To determine the size of an array in C, use the sizeof
operator, which gives the memory size in bytes. Divide the total size of the array by the size of an individual element:
Example:
c
int arr[] = {1, 2, 3, 4, 5};
size_t size = sizeof(arr) / sizeof(arr[0]);
printf("The size of the array is: %zu\n", size);
Explanation:
sizeof(arr)
gives the total memory size of the array.sizeof(arr[0])
gives the size of one element.- The division yields the number of elements in the array.
Note: This works only for arrays declared in the same scope, not pointers.