Join us Now |  Forgot Password? | Forgot UserName?


Learn step by step


What is an Array

An array in C Language can be defined as number of contiguous memory locations, each of which can store the same data type and which can be references through the same variable name.

An array is a collective name given to a group of similar type of quantities. These similar type quantities could be percentage marks of 200 students, number of chairs in home, or salaries of 200 employees or ages of 100 students, etc. Thus an array is a collection of similar data elements. These similar data elements could be either all integers or all floats or all characters etc. Generally, the array of characters is called a "string"( or we can say 'string' is an array of character type), where as an array of integers or floats is called simply an array. All elements of a given array must be of the same data type i.e we can not have an array of 10 numbers, in which 5 are integer and 5 are floats.

Arrays and pointers have a very special relationship as arrays use pointers to reference memory locations.

Declaration of an Array

Arrays must be declared before they can be used in the program. Syntax of standard array declaration is:

type variable_name[lengthofarray];

Here type specifies the variable type of the element which is going to be stored in the array. In C language we can declare the array of any basic standard type which C language supports.

double height[10];
float width[20];
int min[9];
char name[20];

In C Language, arrays starts at position 0. Elements of the array occupy adjacent locations in the memory. C Language treats the name of the array as it were a pointer to the first element This is important in understanding how to do arithmetic with arrays. Item in the array can be accessed through its index, and it can be accessed any where from with in the program.


variable t will have the value of first item of array height.

The below given program will declare an array of five integers and print all the elements of array.

int myArray [5] = {1,2,3,4,5};
/* To print all the elements of the array
for (int i=0;i<5;i++){
printf("%d", myArray[i]);
Initializing Arrays

Initializing of array is very simple in c programming. The initial values are enclosed within the curly braces in the declaration and placed following an equal sign after the array name. Below is an example which declares and initializes an array of five elements of int type. Array can also be initialized after declaration. Look at the following C code which show the declaration and initialization of an array.

int myArray[5] = {1, 2, 3, 4, 5}; //declare and initialize the array in one statement
int studentAge[4];
Performing operations on Arrays

Here is a program that will show the simple operations of the array.

#include <stdio.h>
void oneWay(void);
void anotherWay(void);
int main(void) {
/*Array initialized with aggregate */
void oneWay(void) {
int tuton[10] = {1,2,3,4,5,6,7,8,9,0};
int i;
for (i=0; i<10; i++){
printf("i = %2d tuton[i] = %2d\n", i, tuton[i]);
/*Array initialized with loop */
void anotherWay(void) {
int tuton[10];
int i;
for (i=0; i<10; i++)
tuton[i] = i+1;
for (i=0; i<10; i++)
printf("i = %2d tuton[i] = %2d\n", i, tuton[i]);

Multidimensional Arrays

In C Language we can have arrays of any dimension. To understand the concept of multidimensional arrays let us consider the following 4 X 5 matrix




1 2


4 5

Let us assume the name of matrix is x

To access any element from the array we have to use two subscripts one for row number and other for column number the notation is of the form X [i] [j] where i stands for row and j stands for column subscripts. Thereby X [0] [0] refers to 10, X [2] [1] refers to 16 and so on In short multiple dimensional arrays are defined more or less in the same manner as single dimensional, except that for subscripts you require two square brackets.

Below given are some of the typical two-dimensional array definitions

float table [50] [50];
char line [24] [40];

The first example defines tables as a floating point array having 50 rows and 50 columns. And the number of elements will be 2500 (50 X 50).

The second declaration example establishes an array line of type character with 24 rows and 40 columns. The number of elements will be (24 X 40) 1920 consider the following two-dimensional array definition:

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

Values [0] [0] = 0 Values [0] [1] = 1 Values [0] [2] = 2 Values [0] [3] = 3
Values [1] [0] = 4 Values [1] [1] = 5 Values [1] [2] = 6 Values [1] [3] = 7
Values [2] [0] = 8 Values [2] [1] = 9 Values [2] [2] = 10 Values [2] [3] = 11

Here the first subscript stands for the row number and second for column number. First subscript ranges 0 to 2 and there are altogether 3 rows second ranges from 0 to 3 and there are altogether 4 columns.

In the alternative above definition can be defined and initialized as

int values [3] [4] = {
0, 1, 2, 3
4, 5, 6, 7
8, 9, 10, 11

In the above example the values in first pair of braces are initialized to elements of first row, and the values of second pair of inner braces are assigned to second row and so on. Note: outer pair of curly braces is required. If there are few values remaining within a pair of braces the remaining elements will be assigned as zeros (0)

Here is a simple program that stores roll numbers and marks obtained by a student in matrix

main ( )
int stud [4] [2];
int i, j;
for (i =0; i < =3; i ++)
printf ("\n Enter roll no. and marks");
scanf ("%d%d", &stud [i] [0], &stud [i] [1] );
for (i = 0; i < = 3; i ++)
printf ("\n %d %d", stud [i] [0], stud [i] [1]);

The above example shows how a two-dimensional array can be read and how the values stored in the array can be displayed on screen.

Related Videos