Lets write c program to find number of elements present in an array, where the array size is not mentioned explicitly.
Related Read:
Sizeof Operator in C Programming Language
Page Contents
int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23, 56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8};
We have 25 no of elements in array.
Method 1
#include<stdio.h> int main() { int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23, 56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8}; int count = sizeof(a) / sizeof(int); printf("We have %d no of elements in array\n", count); return 0; }
Output:
We have 25 no of elements in array.
We know that array variable name contains base address or the address of first array element. But sizeof(array_name) gives back the number of bytes occupied by the entire array. Now divide this value by the memory occupied by 1 element – this can be determined either by using sizeof() on the data type(as shown in above source code) or any of the element in the array(as shown in below source code).
Method 1
#include<stdio.h> int main() { int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23, 56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8}; int count = sizeof(a) / sizeof(a[0]); printf("We have %d no of elements in array\n", count); return 0; }
Output:
We have 25 no of elements in array.
We assume that the array will at least have 1 element, so we find the number of bytes occupied by it. This is the best approach because, this way we’ll not be compelled to change the data type passed to sizeof() operator whenever we need to check the number of elements present in a array of different data type.
Method 3: Using Macros
#include<stdio.h> #define SIZE(a) ( sizeof(a) / sizeof(a[0]) ) int main() { int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23, 56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8}; printf("We have %d no of elements in array.\n", SIZE(a)); return 0; }
Output:
We have 25 no of elements in array.
Here we pass the array variable name to macro SIZE(). Again the macro expansion will be replacing the macro template before passing the source code to the compiler.
Related Read:
Macros With Arguments: C Program
Using this method has advantage too. We can calculate size of multiple array variables, of different data type: as shown in below example.
Method 4: Using Macros
#include<stdio.h> #define SIZE(a) ( sizeof(a) / sizeof(a[0]) ) int main() { int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23, 56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8}; char ch[] = {'i', 'P', 'h', 'o', 'n', 'e'}; double dbl[] = {2.3, 4, 5.2, 6.3, 7.1, 3.4, 1.3}; printf("Integer variable a[%d].\n", SIZE(a)); printf("Character variable ch[%d].\n", SIZE(ch)); printf("Double variable dbl[%d].\n", SIZE(dbl)); return 0; }
Output:
Integer variable a[25].
Character variable ch[6].
Double variable dbl[7].
Here we find the number of elements present in array variable a and ch using the same macro SIZE.
Whenever we want to iterate through individual elements of the array and we’ve no idea whats the size of the array or whats the largest designator of that array. In such cases we can’t write condition inside loop.
Using above formula we could easily find the number of elements present in the array and make use of the result in our for loop condition:
#include<stdio.h> #define SIZE(a) ( sizeof(a) / sizeof(a[0]) ) int main() { int a[] = {3, 4, 6, 7, 8, 9, 0, 32, 435, 65, 2, 23, 56, 67, 8, 9, 0, 3, 2, 1, 4, 5, 6, 7, 8}; int count = SIZE(a); for(i = 0; i < count; i++) printf("%d\n", a[i]); return 0; }
This prints all the elements of the array.
Note: Do not write i < SIZE(a) condition directly inside for loop. This would create unnecessory overhead, as for each iteration SIZE(a) [which will be replaced by ( sizeof(a) / sizeof(a[0]) ) while preprocessing] has to calculate the size of the array. Instead calculate the size of array once and store it inside a variable and then use that variable in your for loop as shown in above source code.
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert