BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Find the Number of Elements in an Array

Last Updated: September 24, 2017 by Chaitanya Singh | Filed Under: C Programs

Here we will write a C program to find the number of elements in a given array.

Example: Program to find the size of an array

The formula that we are using to find the number of elements is common for all types of array. In this example, we have an array of double data type, however you can use the same logic for arrays of other data types like: int, float, long, char etc.

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

Output:
Size of the array is: 6

Check out the related C Programs:

  1. C Program to find largest element of an array
  2. C Program to find sum of array elements
  3. C Program to display a number entered by user
  4. C Program to check if a number is palindrome or not

Top Related Articles:

  1. C Program to find greatest of three numbers
  2. C Program to concatenate two strings without using strcat
  3. C Program to calculate Area of an Equilateral triangle
  4. C Program to swap first and last digit of a number
  5. C Program to Convert Decimal Number to Binary Number

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap