Array in C – Declare Initialize Access and Operations

Here We learn Array in C how to Declare Initialize and access array and operations on array.

Array is a data structure that can store elements of the same type.

Array is a collection of similar type elements

It stores the elements in a contiguous memory locations.

1 Why Array Required?

Array is used to store similar types of values under same name

 We want to declare 100 integer variable

Instead of declaring 100 individual variables, such as

int number0, number1,number2,number3, ... …., number99;

you can declare one integer array variable such as

int number[100];

here number[100] is an integer array of size 100, means this array can store 100 integer value.

Array indexing starts from index 0 to n-1. Means first integer number store in array number[0], second integer number store in array number[1], third integer number store in array number[2], and so on. Here 100th  number store in array numbers[99].

2 How to Declare an Array in C?

Syntax of declare an array in C is as below

DataType  arrayName [ arraySize ];

This is called a single-dimensional array.

DataType : any valid C data type like int, char, float, double, long
array Name: Any valid c identifier
arraySize: an integer constant greater than zero.

For Instance

  1. int age[5];
  2. long length[8];
  3. float price[7];
  4. double value[5];
  5. char ch[5];

Above all are example of array.

  1. age is a integer array of size 5.
  2. length is a long array of size 8
  3. price is a float array of size 7
  4. value is double array of size 5
  5. ch is a character array of size 5

Size tells us that a array can store maximum element up to size.

Size must be a integer constant. based on size array occupies memory location for elements.

There are multi dimensional array.

3 Array Initialization

Giving initial value to array is know as array initialization.

A Initializing each member

We can initialize each element one by one as below.

int age[5]; //declaration


Array uses zero based indexing so first element is stored in age[0] and last element will store in size-1 position here age[4];

B Declare and initialize

general syntax is

DataType arrayName[size]={value1,value2,.............,valueN};


int age[5]={21,24,25,27,26};

value 21 will store in age[0] and value 24 will store in age[1] and similar for other values.

While initializing array giving size is optional;


float price[]={22.2,44.2,67.33,55.66,43.2};

Some times we want to initialize only few elements of array in that case we have to provide size of array and initialize only few values other values are initialized by zero.

double value[20]={10.0,21.0};

Here from value[3] to value[19] will be initialize by 0.0

4 Access Array Elements

Array uses zero based indexing to access elements.

to access element of an array, we can use syntax


int age[5]={21,24,25,27,26};

age[0], age[1],age[2],age[3],age[4], will access 21,24,25,27 and 26 respectively.

Lets see few examples to store values in array and access array values.

Example: write a program to create integer array and store 5 integer number and print.  

Description : In the above program,   int  num[5] array can store 5 element in a contiguous memory locations from index 0 to 4.         

Example: write a program to take 5 number from user and store integer number in integer array and print.  


Example: Print the sum of all elements of an array


5 Operations on Array

On array we can perform following basic operations

  1. Traverse– accessing all element of array
  2. Insertion– adding a new element at specified position
  3. Update – changing the existing value at specified position
  4. Deletion– deleting an element from specified position
  5. Search– search a element in array

A Traverse an array

Traversing is accessing the element of array.

Here we will access each element and print it


B. Inserting an element in Array

add a new element in array can be done at various position

  • Insertion at the beginning of array
  • add at the give index position
  • Insert at last

Here we will insert a element based on index position.

On inserting a new element at specified position next element will move one space forward in array.


C Update an element in Array

To update an array element we have to select element based on index value and provide new value to replace the existing one.

Simple program to update array value based on index is as below


D Delete an element in Array

To delete an array element First select index value of element.

After deleting array element other elements are shifted forward to fill the gap of deleted element.


E Search an element in Array

Search operation is used to find an element in array.

If element fond return index and element value.

If not it will return -1.


F Sorting elements in Array

Arranging array elements in ascending or in descending order is known as sorting of array.

Following program is used to sort array elements in ascending order.


Addition of Two Matrix in C Programming

A Matrix is Two dimensional array that have number of rows and columns. Addition of Two Matrix in C Programming is very easy.

To write program for adding two matrix is same as we perform matrix addition in mathematics.

To Add Two Numbers rows and columns of both matrix should be same.

Element of each row and column of matrix A will added with same row and column element in matrix B.

The sum of both matrix will produce same row and column matrix in resultant matrix.

Example: Write a program to add two matrix. Firstly, ask from user order of matrix( number of rows and column).

Then take the elements of matrix from user as a input and print the resultant matrix

For example,

if a user input order as 3,3, i.e., three rows and three columns and
First matrix                      
1             2             3
4             5             6
7             8             9

Second matrix:
9             8             7
6             5             4
3             2             1

then the output of the program (addition of two 3*3 matrix in c) is:

10           10           10
10           10           10
10           10           10

Steps for Addition of Two Matrix in C Programming

  1. Declare Three matrix f,s and sum.
  2. Take elements for matrix f and s.
  3. Add both matrix f and s and save to sum matrix.
  4. Print the sum matrix.

Matrix addition program in C

Number of rows and columns are controlled by variable m and n.

To avoid checking again row and column of both matrix it is accepted at only one place and used by both matrix.

Both Matrix elements are taken from user as per requirement user can also declare matrix elements in program.


Hope you got how to add two matrix in C. Similar way you can also write program to multiply two matrix.

Multi-dimensional Array in C Programming

C programming language also support multidimensional arrays.

Syntax of multidimensional array declaration:

Data_type    array_name[size1][size2]…[sizeN];

For example, if we want to creates a three dimensional integer array −

int  num[5][10][4];

Two-dimensional Arrays

The simplest form of multidimensional array is the two-dimensional array.

if we want to declare a two dimensional integer array of size [x][y] ( where x is a  number of rows and y is a number of columns ) you would write something as follows −

 int  a[3][4];

In the above line we have created two-dimensional integer array ”a” with 3 rows and 4 columns.

In a C programming language Multidimensional arrays may be initialized by specifying bracketed values for each row.

For example we want to create an integer array with 3 rows and each row has 4 columns.

Above the nested braces indicates the intended rows.

The following initialization is equivalent to the previous example −

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

which indicate the nested braces for intended row, are optional.

Accessing Elements of Two-Dimensional Array

Create a two-dimensional array of size 3*3 and  nested loop is used to handle a two-dimensional array −

Example: Write a program to create 3*3 matrix and take all elements of matrix as an input from user and print it.

Array in PHP

An array is a data structure or a collection of data that contains a group of elements under same name.

Typically these elements are all of the same data types or different data types.

for example:
If a complex has thirty flats each flat having same features then the whole complex is considered to be an Array.

Each of thirty flat is an element of that array and as each flat shares the same type of features – those are considered as same data types.

So, in a programming language like PHP, the array is a collection of different type of value in a single variable.

You also can say a string is one kind of array because string consists of multiple characters.

Note: In programming languages like ‘C’, only one type of value (integer or string or float etc.) can be stored in an array and must be defined at the beginning of the program, but in PHP we can store different types of value (string, integer, Boolean, null, object, float) in a single array.

Define an array

An array is a data structure that stores one or more different type of values in a single variable.

Syntax of an array

Note: Most of developers use syntax type II for robust programming support.

How to print array in PHP

How to print an array in PHP?

Answer is very simple declare and initialize array use print_r() function to print array.

array elements are stored in continuous memory location

Note: this two functions (print_r() and var_dump()) are also debugging function in PHP (about debugging function will be discussed later)


now 0 1 2 3 are index of the value means 0 1 2 3 in this position apple mango banana lemon are stored respectively.

Index means which is used to identify the particular one value/element in the array.

We can find a house by its address but when we try to find a room in an apartment there must be a number or floor division which separates every room/house from each other rooms/houses.

In similar way multiple value stores in single array separates by a unique index number.

It is also called an ‘ARRAY IDENTIFIRE’. Index, always starts from 0 and automatically incremented, but you also set index manually.

You can return particular one element of any array by using index number of this array.

In another way you also define array in PHP:

Display an array

This is the example of manual index value. We can also use in this manner.

How to know the length or size of an unknown array


Printing array using loop in PHP


Using FOREACH loop


How to store values in array in php using for loop

How to store values in array in php using forEach loop

How to store values in array in php using while loop

Output of all above is same