Skip to content
Home » How to determine size of array in C

How to determine size of array in C

Learn about C Program to determine size of array in C in the below code example.
Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C Program to determine size of array in C:

Size of array is determined using function sizeof() function. After Declaring a array, Prdefined function namely, sizeof() function is called passing array as parameter.

#include<stdio.h>
int main()
{
    int array[10];//declaring array
    size_t a=sizeof(array);//storing size of array in a
    printf("%d",a);
}

40

Using This sizeof() function, the output is obtained in bytes. In above example number of elements array is 10 and declared array consists of integer and hence each integer size is 4bytes.Thus total size of array in returned as 4*10 bytes. Let us consider case of char array.

#include<stdio.h>
int main()
{
    char array[10];
    size_t a=sizeof(array);
    printf("%d",a);
}

10

As the above example array is declared with char elements, char is defined to occupy 1byte and hence overall size of array is 1*10bytes.

sizeof(array[0]);//gets size of 1st element in array

Each Element is obtained by passing the pointer of that index of array.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Also Read:

Initializing array with same value