原文: https://beginnersbook.com/2017/09/c-program-to-find-the-number-of-elements-in-an-array/
这里我们将编写一个 C 程序来查找给定数组中的元素数。
示例:用于查找数组大小的程序
我们用于查找元素数量的程序,对于所有类型的数组都是通用的。在这个例子中,我们有一个double数据类型的数组,但是你可以对其他数据类型的数组使用相同的逻辑,如:int,float,long,char等。
#include <stdio.h>int main(){double arr[] = {11, 22, 33, 44, 55, 66};int n;/* Calculating the size of the array with this formula.* n = sizeof(array_name) / sizeof(array_name[0])* This is a universal formula to find number of elements in* an array, which means it will work for arrays of all data* types such as int, char, float etc.*/n = sizeof(arr) / sizeof(arr[0]);printf("Size of the array is: %d\n", n);return 0;}输出:
Size of the array is: 6
查看相关的 C 程序:
