Array in C programming with example

What is an array?

An array is a collection of similar data elements that are stored under a common variable name. It is a group of similar data types. It is a collective name given to a group of equal quantities. A set of values ​​of the same type is treated as an array.

Example of an array:

  • Marks of 100 students.
  • Salary of 500 lecturers.
  • Ages of 50 professionals.


storage_class data_type array_name [expression] 


1. storage_ class

Specifies the scope of the array variable such as external, static, or automatic.

2. data_type

Specifies the nature of the data element stored in the array.

3. array name

Specifies the name of the array.

4. expression

Specifies the size of memory location required for further processing by the program.

auto char name_of_student[30]
an auto is a storage class
char is a data type
name_of_student is the name of the array and
30 is the size of an array

A specific element in an array is accessed by an index. In C, all arrays have contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. An array can have multiple dimensions.


  • The first element in the array is 0, so the last element is 1 less than the size of the array.
  • An array is also known as a subscripted variable.
  • Array type and its dimensions must be declared before use.
  • Array elements are always stored in contiguous memory locations.


Let us consider a program:

#include <stdio.h>
#include <stdlib.h>
int main()
int i; 1-2; i=3;
printf("Updated Value of i in memory %d", i);
return 0;


Updated Value of i in memory 3

The old value of i is replaced by its new value. With this program, it is clear, that a simple variable is capable of holding only one value at a time.

Suppose an employer wants to arrange 200 employee’s salary, then he will have two choices:

  • To construct 200 ordinary variables.
  • To construct only one variable capable of storing 200 values.

Which option is better? The answer would be the second option because it is very easy to handle a variable and their logic.

Therefore, this single variable capable of storing more than one value is known as an array.

Characteristics of Array

  1. An array is also known as a subscripted variable.
  2. Its type and dimension must be declared before the array can be used.
  3. An array holds elements that have the same data type.
  4. The array elements are stored in subsequent memory locations.
  5. Two-dimensional array elements are stored row by row in subsequent memory locations.
  6. Array’s name represents the address of the starting element.
  7. Array size must be mentioned in the declaration. The size of the array must be a static expression and not a variable.
  8. When declaring a 2D array, the number of columns must be specified and is a mandatory one, while there is no such rule for the number of rows.
  9. By default, an array index starts at 0 and ends with an index number (n – 1). So the last element is 1 less than the size of the array.
  10. An array cannot be changed at run time.

Types of an Array

There are three types of an array in C:

  • One-dimensional or 1D Array
  • Two-dimensional or 2D Array
  • Multidimensional Array

One-dimensional Array in C

A linear list of data items of the same type can be considered as a 1D array.

Syntax of 1D Array:

storage_class data_type name_of_array[Size_of_array]; 

The size of an array must be an integer constant only.

Example: (Array declarartion)
int marks[5];
char name[25];
flaot weight[3];

(Initialization of 1D array can be done in this form)
int num[6] = {1, 2, 3, 4, 5, 6}

Two-dimensional Array in C

A matrix of data items of the same type can be considered as a 2D array.

Syntax of 2D Array:

storage_class data_type array_name [dimension of row][dimension of column]; 

The first dimension of the array is considered as row and the second dimension of the array is considered as a column.

Example: (Array declarartion)
int marks[3][3];
flaot weight[3][3];

(Initialization of 2D array can be done in this form)

int marks[3][3] = {60, 40, 36, 82, 70, 52, 30, 20, 67};
marks[0][0] = 60;     marks[0][1] = 40;     marks[0][2] = 36;
marks[1][0] = 82;     marks[1][1] = 70;     marks[1][2] = 52;
marks[2][0] = 30;     marks[2][1] = 20;     marks[2][2] = 67;

Multidimensional Array in C

We can define multi-dimensional arrays in the same way as one-dimensional arrays other than brackets of a different class.

To define a two-dimensional array, we need to take two pair square brackets, while for a three-dimensional array we need to have three pair square brackets and the like.


data_type array_name[exp_1][exp_2]……..[exp_n];

Example: int matrix m[3][3][3];

Initialization of 1D Array

If the array is initialized where it is declared, mentioning the dimension of the array is optional.

Example 1: int num[ ] = (2, 4, 6, 8, 10);
Example 2: char name[ ] = (‘S’,’A’,’N’,’J’,’A,’Y’};

The elements of an array treated by the initialization, in the following form:

num[0] = 2
num[0] = 4
num[0] = 6
num[0] = 8
num[0] = 10
name[0] = 'S'
name[0] = 'A'
name[0] = 'N'
name[0] = 'J'
name[0] = 'A'
name[0] = 'Y'

Array index starts from 0 to n-1, where n is the maximum size of the array declared by the programmer because in C the first element of an array is always placed in the 0th place.


  • The array elements are considered to be garbage values, no specific values ​​are assigned to the sick elements.
  • If it is declared, it is optional to specify the dimension of the array when the array is initialized.

Initialization of 2D Array

1. int x[2][2] = {1, 2, 3, 4};


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




x[0][0] = 1
x[0][1] = 2
x[1][0] = 3
x[1][1] = 4

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


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



x[0][0] = 1
x[0][1] = 2
x[0][2] = 3
x[1][0] = 4
x[1][1] = 5
x[1][2] = 6
x[2][0] = 7
x[2][1] = 8
x[2][2] = 9

But if we want arrangement i.e.


Then array initialization would be:

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

Memory Representation of Array

Memory Representation of 1D Array:

Consider, int num[9];

This declaration causes reserved memory for 9 numbers i.e. 18 bytes (2 bytes each).

Memory representation of 1D-Array


#include <stdio.h>
#include <stdlib.h>
int main()
    int i;
    int value[ ]={3,6,9};
    for (i=0; i<=2; i++)
    printf("Value at index ""%d ""is: %d\n", i, value[i]);
return 0;


Value at index 0 is: 3
Value at index 1 is: 6
Value at index 2 is: 9

Memory Representation of 2D Array:


Example of 2D Array:

#include <stdio.h>
#include <stdlib.h>
int main()
    int arr[3][3]={1, 2, 3, 4, 5, 6, 7, 8, 9};
    int i,j;
    for(i=0; i<=2; i++)
       for (j=0; j<=2; j++)
           printf(" %d", arr[i][j]);
return 0;


 1 2 3
 4 5 6
 7 8 9

Array Declaration

An array can be declared in the following way:


Valid Declarations of an Array:

  • int arr[35];
  • float age[20];
  • char name[15];

Invalid Declarations:

  • int marks[0];
  • float age[-30];
  • char name[$];
  • float coordinate[0.02];

Previous Lecture

Loops in C
Operators in C
Data Types in C
Switch Case Statement

Conditional Statements if,if-else
Jumping statements goto, break, etc.

If you have any problem in solving your questions, then join our Telegram group or search @Learnsimply, we will help you.

Please write comments if you find anything incorrect in the Array in C lecture.

Leave a Comment

en English