The array length can be obtained through the sizeof operator. The formula is: sizeof (array name) / sizeof (array element type). The result is the array length.
How to find the length of C language array
Direct method
<code class="c">#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int len = sizeof(arr) / sizeof(arr[0]); printf("数组长度:%d\n", len); return 0; }</code>
sizeof operator
In C language, the sizeof
operator can obtain the size of a variable or data type. For arrays, sizeof
returns the total number of bytes occupied by all elements in the array. The array length is obtained by dividing this by the size of the individual elements.
Other methods
The above method is the standard method, but in some cases, other methods can also be used to find the array length:
_countof
(Visual C) or __builtin_object_size
(GCC). These macros directly return the array length. The above is the detailed content of How to find the length of an array in C language. For more information, please follow other related articles on the PHP Chinese website!