How to find the length of an array in C?

How to find the length of an array in C?

a) sizeof(arr)
b) sizeof(arr) / sizeof(arr[0])
c) length(arr)
d) arr.size()

Answer:

b) sizeof(arr) / sizeof(arr[0])

Explanation:

In C, the length of an array can be determined using the expression sizeof(arr) / sizeof(arr[0]). This works by dividing the total size of the array (in bytes) by the size of a single element, giving the number of elements in the array. This method only works for arrays whose size is known at compile-time.

Understanding how to correctly calculate the length of an array is important for avoiding out-of-bounds errors in C programming.

Reference links:

https://www.rameshfadatare.com/learn-c-programming/

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top