# C 程序：查找数组中的元素数

> 原文： [https://beginnersbook.com/2017/09/c-program-to-find-the-number-of-elements-in-an-array/](https://beginnersbook.com/2017/09/c-program-to-find-the-number-of-elements-in-an-array/)

这里我们将编写一个 C 程序来查找给定数组中的元素数。

## 示例：用于查找数组大小的程序

我们用于查找元素数量的程序，对于所有类型的数组都是通用的。在这个例子中，我们有一个`double`数据类型的数组，但是你可以对其他数据类型的数组使用相同的逻辑，如：`int`，`float`，`long`，`char`等。

```c
#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;
}

输出：
```

```c
Size of the array is: 6
```

查看相关的 [C 程序](https://beginnersbook.com/2015/02/simple-c-programs/)：

1.  [C 程序：查找数组的最大元素](https://beginnersbook.com/2015/02/c-program-to-find-largest-element-of-an-array/)
2.  [C 程序：查找数组元素之和](https://beginnersbook.com/2014/06/c-program-to-find-sum-of-array-elements-using-pointers-recursion-functions/)
3.  [C 程序：显示用户输入的数字](https://beginnersbook.com/2017/09/c-program-to-print-an-integer-entered-by-a-user/)
4.  [C 程序：检查数字是否是回文](https://beginnersbook.com/2015/02/c-program-to-check-if-a-number-is-palindrome-or-not/)