Skip to main content

Arrays in C Language

C Language Beginner level

Array is a set of similar Data Type values . We can also say an Array is like a box that holds only the values or things of same type . Array can also be reffered as the Linear Data Structure because of the linear fashion while storing data values and User defined Data Type also .

  • An Array always contains a contigous set of all values inside the memory .
  • In array we use indexing starting from 0 . Thus if we have total n elements in the array , then we say the 1st element is 0th element and nth element is (n-1)th element . 
We can understand the working of an array by the below diagram - 

Arrays in c language
 How to declare an array in C : 

If we want to declare an array " Box " with size of 10 elements of integer data type , we use the syntax as - 

int Box[10] ;

Note :

  • Here we dclared the Array and can put values inside in further code anywhere .Here Box is the Array name and 10 is the size of Array and int Data Type tells that all elements of the array are integers .
  • Here 10 is the Maximum size of this array and this Array can not contain the elements more than 10 .

How to Initialize an Array in C : 

If we want to put some elements in that array , It is called Array Initialization .

int Box[10] = { 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 };

Here we filled this array with the values from 1 to 10 .

How to access Array Elements : 

If we initialized an array before and want to access the elements from that array , suppose we want to access the 6th element from array Box , we write the code as -

int x = Box[5] ; 

Here we accessed the 6th element from Box[] and stored that value temporarily in the another variable x . We use the ( n-1 ) index to access the nth element , So we used 5 to access 6th element .

Note :
If we want to access all the elements of array , we use the Loop code rather than writing Box[0] , Box[2] , Box[3] , ........, Box[9] . We use here the For loop and can write the entire code as -

#include <stdio.h>
void main()
int Box[10] = { 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 };

for(i=0 ; i<10 ; i++)
printf( " Array elements are \n %d \n " , Box[i] ) ;

return 0 ;

Here for loop shows that we are accessing array elements from i=0 to i=9 . Here i is only a counter to count all elements one by one .

Let's see the Loops in detail in the next Page .

                                                       << Back            >>Next