Arrays in C

Introduction to array

Array in C language is kind of like a list in python. An array is a collection of similar type of element storable together in a single contagious memory location.The important thing to remember is the all the data elements stored in an array should be of similar data type.

Array is a reference type. which uses the Heap Area of memory to store the same type of data items and uses continues memory locations to store every data item of array. Therefore, all data items in array are stored in a certain order in memory and for each element to identify uniquely, a Zero Based Index Number is specified with it. Also Array is a fixed Data Structure. So once the size of the Array is defined, then the size of array can not be modify.

  • Whatever the data type of the array can be.
  • The variables of array are stored on their near memory location.
  • The initialization of array starts with ‘0’.

Creating C array:-

Whenever you create an array, you define the name of the array and how many values it is going to store in it.

As if you want to store 4 numbers, you can create an array for that. the general structure of creating arrays  in C is given below.

data_type array-name[size];

By size, you define how many values you want to store.

int sum[5]

In this you can store any 5 integer values in it.

Initializing C array

The initialization of an array by using index , we can initialize each element of array by using index.

here’s the example:-

sum[0]=30;  // initialization of array

C Array Example:-

int main()
int i=0;
int sum[5]; // declaration of array
sum[0]=30;  // initialization of array
// traversal of array
//end of the loop
return 0;



Types Of Array

  • One or Single Dimensional Array
  • Multi-Dimensional Array.

Single Dimensional Array

In an array we don’t need to declare each and every element we only need to declare the array, the type of data in the array and the size of the array.Let’s see an example for declaring and saving element in the array and finally printing the elements saved.

int main()
   int arr[5];       //declaring the array
   int i,a;
   for(i=0;i<5;i++)    //loop to take input in the array from user
   for(i=0;i<5;i++)    // loop to print the elements of the array
   return 0;

Output :


Note : An array of size 5 can store 5 element but the 1st element is stored in the zeroth index value and then 1st, 2nd, 3rd, and last at 4th.And this goes for every array.

Initializing one dimensional array:-

There are a bunch of way to store data in an array, like

int marks[5];


int marks[5]={19,11,14,8,45};

Both of these are the same array with the same elements.This type of liner array is called 1-D array 

Multi Dimensional Array

A multi-dimensional array or a 2-D array is a similar to 1-D array but the element stored in a 2-D array are stored in a matrix format rather than a linear manner. the elements are stored in rows and columns like a matrix.

The simplest form of multi-dimensional array is 2-D array.

Initializing of  2-D Array:-

Let’s see an example to understand.

int arr[3][4]={1,2,3,4,2,3,4,5,3,4,5,6}; or        //this array has 3 rows and 4 columns

int arr[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};

Here’s the array will be like:-

1 2 3 4
2 3 4 5
3 4 5 6

Example of Multi-dimensional array:-

int main()
    int arr[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};
    int i,j;
    return 0;



Most of the array is created in 1D or 2D array.