C Program to Find the Number of Elements in an Array (JNNC Technologies)





                                                          http://jnnctechnologies.com/

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
 
 
 
_________________________________________________________________________________
Java Training Institutes in Vizag | Hardware Training Institutes in Vizag | 
Computer Institutes in Vizag | Computer Training Institutes in Vizag 
|Coaching Centers in Vizag |Coaching institute for java and c  best 
computer courses | best computer courses list | java institutes in vizag
 |Software testing training institutes in Visakhapatnam. 
__________________________________________________________________________________
 

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();