Showing posts with label sizeof. Show all posts
Showing posts with label sizeof. Show all posts

Wednesday, 27 February 2019

Finding number of elements in an array

Problem: Given an array of integers ,find the number of elements in it.
Examples:
       Input : array[]={76,90,87,98,56,77,91}
       Output :
          No of Elements :7
         
//C program to find the number of elements in an array
#include<stdio.h>
    int main(){
       int array[]={76,90,87,98,56,77,91};
       int arraySize=0;

      /*key here is sizeof operator which return the size in byte: sizeof(array) 
       determines the size of array in bytes and sizeof(array[0]) determines of first 
       element in byte.*/

      //finding the size(no of elements) of array
       arraySize = sizeof(array)/ sizeof(array[0]);

       printf("Number of Elements in the array are %d.\n",arraySize);
  return 0;
}
Output:













Please comment if you find anything incorrect, or you want to improve the topic discussed above.