sizeof() operator returns the total number of size occupied by a variable, since array is also a variable, we can get the occupied size of array elements.
#include <stdio.h> int main() { int arr[]={10,20,30,40,50}; int n; n=sizeof(arr)/sizeof(int); printf("Number of elemenets are: %d\n",n); return 0; }
Output
Number of elemenets are: 5
We can divide the occupied size of all array elements by size of any one array element. Consider the following statement:
n=sizeof(arr)/sizeof(arr[0]);
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Program to count total number of array elements in C
Output
Another method
We can divide the occupied size of all array elements by size of any one array element. Consider the following statement:
need an explanation for this answer? contact us directly to get an explanation for this answer