This is a C Program to find the number of elements in an array.
This C Program finds the number of elements present in the given array.
1. Create an array along with the definition of all its elements.
2. Create a variable which will hold the size of the array.
3. Using the inbuilt function sizeof(), passing the name of the array whose size we need to calculate, returns the number of elements contained by array.
4. The returned value is stored in the variable created above.
Here is source code of the C Program to find number of elements present in the given array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to Find the Number of Elements in an Array
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int array[] = {15, 50, 34, 20, 10, 79, 100};
int n;
n = sizeof(array);
printf("Size of the given array is %d\n", n/sizeof(int));
return 0;
}
1. Declare an array and define its elements at the time of declaration.
2. Create a variable n, which will store the size of the array, i.e total number of elements present in array.
3. Now, call an inbuilt function sizeof() (from unistd.h library) passing the name of the array as its parameter.
4. This function will return the number of elements present in the array passed to it.
5. Store it in variable n.
Size of the given array is 7
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Apply for C Internship
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Practice BCA MCQs
- Check Computer Science Books