DEV Community

Divyansh Pratap Singh
Divyansh Pratap Singh

Posted on • Updated on

finding size of Array in C

What we will learn ?

In this read you are going to learn how to find the size of the array in C programming language and the concepts behind its working .

Introduction :

As in any other programming language like Java we have length property of array through which we can easily find out the size or the length of the array but in c arrays do not have any such properties through which we can easily find out the size of the array . Though there are two ways through which we can find out the length or size of the array in our C programming language ; but in comparison to other languages like of java it's a bit complicated . So , in this blog you are going to learn how to find the size along with the working and the reason why it is happening so .

Method 1

Using a constant variable size

So , we will be defining an constant variable 'Size' which would store the size or the length of the array . Suppose we have to store five values in our array so the value of our size variable is 5 .

const  int Size  = 5 ;
Enter fullscreen mode Exit fullscreen mode

Here , in the above line const is a keyword that declares the variable Size as constant i.e we cannot change the value of Size in the upcoming program .

Now , the next step is to define and initialize our array.

 int arr[Size] = { 1 , 2 , 3 , 4, 5};
Enter fullscreen mode Exit fullscreen mode

here ; in the above line we declared an array of name 'arr' and passed the values inside the array ; also do remember to pass the size of the array .

code of c program to print the elements of Array :

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();

const int size = 4;
int arr[size] = {1,2,3,4};

for(int i = 0 ;i<size ;i++){
printf("%d \n " , arr[i]);
}
getch(); }
Enter fullscreen mode Exit fullscreen mode


Additional info :

Case 1 :

 const  int  size = 4;
  int arr[size] = { 1,2,3 };
Enter fullscreen mode Exit fullscreen mode

In this code we have set the size of the array as 3 and inside the array we are only passing 3 values . Will we get an Error ?


No , actually what happens when we declared size as 4 then there are 4 blocks created where you are only entering values in 3 blocks so , the last block remains empty . So , the empty block is filled by 0 by default during the run time ._

Case 2 :

  const  int  size  = 2 ;
int arr[size] = {1,2,3}; 
Enter fullscreen mode Exit fullscreen mode

here , in the above code we have set our size as 2 so now we have 2 blocks and later in the program we are storing 3 values ; we have 2 blocks and we are storing 3 values so this is going to throw us an error ._



Method 2

Using sizeof operator

So , In the above section we have learnt how to find out the size of array using a constant variable and now in this section we are going to find out the size or length using sizeof operator .

So , before jumping in to the implementation part first let's us understand what is sizeof operator .

The sizeof is an unary operator in c and c++ . It is basically used to compute the size of the operand .The result of sizeof operator is an unsigned integral type which is denoted by size_t .
sizeof operand can be used with any data type , primitive data type example int , float as well as compound data type example Structures .

Usage

when sizeof() is used with datatypes like int or float then it returns the memory occupied by them .

printf("%lu" , sizeof(int));
Enter fullscreen mode Exit fullscreen mode

this would give 4 as an output

when sizeof() is used with an expression then it would return the size of the expression .

float  a = 1.2;
double b = 1.0;
printf("%lu" , sizeof(a+b));
Enter fullscreen mode Exit fullscreen mode

this would give 8 as an output.



Returning back to our Question :

 int arr = [1,2,3,4];
Enter fullscreen mode Exit fullscreen mode

here in the above code i had declared and initialized an array and now i want to print the size of the array , so let's find out the size using sizeof() operator .
So , for better understanding i am storing the size in a variable named size , if you wish then you can directly print the size .

int arr[] = {1,2,3,4,5};
int size = sizeof(arr) / sizeof(arr[0]); 
Enter fullscreen mode Exit fullscreen mode

Now , you might be wondering why i had divided the sizeof(arr) with sizeof(arr[0]) , let me explain .

sizeof(arr) operator will give you the total memory occupied by the array arr and similarly sizeof(arr[0]) will return you the memory occupied by the single element so by dividing the total space by the space ocupied by 1 element then we will get the number of elements .

You can understand this way , suppose a guy paid 50 INR in total for buying chocolates , now you are told that 1 chocolate was for 10 INR and now tell me the number of chocolates the guy bought .
(40/10) which is 4 chocolates . This very same concept we are using there in sizeof().

Important : sizeof operator do not returns size it returns the memory occupied by the operand .

Entire code for printing the size of Array using sizeof operator.

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int arr[] = {1,2,3,4,5};

int size = sizeof(arr) / sizeof(arr[0]);

printf("%d" , size);
getch()  };
Enter fullscreen mode Exit fullscreen mode

Thankyou , for giving it a read I hope you understood the concept and if you have any doubt please comment below .

Top comments (2)

Collapse
 
pauljlucas profile image
Info Comment hidden by post author - thread only accessible via permalink
Paul J. Lucas • Edited

Your code is poorly formatted and so hard to read. Use Markdown code fences (look it up). It also has syntax errors. You also never mention that sizeof doesn't work for arrays for function parameters.

Collapse
 
devdivyansh profile image
Divyansh Pratap Singh

Thankyou !! for mentioning that

Some comments have been hidden by the post's author - find out more