Q:

C program to count Array elements by using sizeof() operator

0

C program to count Array elements by using sizeof() operator

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to count total number of array elements in C

#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

Another method

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]);

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to swap first element with last, second ... >>
<< Initialising byte array with Decimal, Octal and He...