C Program To Find Size of An Array

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

Example: Expected Output

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.

Video Tutorial: C Program To Find Number of Elements In An Array


[youtube https://www.youtube.com/watch?v=RMJzZXvr2mY]

YouTube Link: https://www.youtube.com/watch?v=RMJzZXvr2mY [Watch the Video In Full Screen.]

Source Code: C Program To Find Size of An 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.

Where Is It useful?

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

Sizeof Operator in C Programming Language

Lets write a C program to see how to use sizeof() method or function in C programming language.

sizeof() is a builtin method/function present in C programming. It is used to calculate the size(in bytes) that a datatype occupies in the computers memory.

sizeof() method takes a single argument and it is not a function whose value is determined at run time, but rather an operator whose value is determined by compiler – so it’s called as compile time unary operator.

sizeof() method can be used with primitive data type like int, float, char or user defined data types like structures, unions etc.

Video Tutorial: Sizeof Operator in C Programming Language


[youtube https://www.youtube.com/watch?v=Mlw4jJ4WX8A]

YouTube Link: https://www.youtube.com/watch?v=Mlw4jJ4WX8A [Watch the Video In Full Screen.]

Source Code: Sizeof Operator in C Programming Language

#include < stdio.h >

int main()
{
    int a, a1[10];
    char s;
    char s1[10];
    int b = 10, c = 5;

    printf("Size of a single character is %d byes\n", sizeof(s));
    printf("Size of a character array s1[10] is %d byes\n", sizeof(s1));
    printf("Size of a integer is %d byes\n", sizeof(a));
    printf("Size of a long integer is %d byes\n", sizeof(long int));
    printf("Size of a long long integer is %d byes\n", sizeof(long long int));
    printf("Size of a integer array a1[10] is %d byes\n", sizeof(a1));
    printf("Size of a float is %d byes\n", sizeof(float));
    printf("Size of a double is %d byes\n", sizeof(double));
    printf("Size of a long double is %d byes\n", sizeof(long double));
    printf("Size of a b+c is %d byes\n", sizeof(b+c));

    return 0;
}

Output:
Size of a single character is 1 byes
Size of a character array s1[10] is 10 byes
Size of a integer is 4 byes
Size of a long integer is 4 byes
Size of a long long integer is 8 byes
Size of a integer array a1[10] is 40 byes
Size of a float is 4 byes
Size of a double is 8 byes
Size of a long double is 12 byes
Size of a b+c is 4 byes

short, int, long int, long long int

#include < stdio.h >

int main()
{
    printf("Size of short data type is %d\n", sizeof(short));
    printf("Size of integer data type is %d\n", sizeof(int));
    printf("Size of long integer data type is %d\n", sizeof(long int));
    printf("Size of long long integer data type is %d\n", sizeof(long long int));

    return 0;
}

Output:
Size of short data type is 2
Size of integer data type is 4
Size of long integer data type is 4
Size of long long integer data type is 8

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