C-Pointers: Previous                                                         Next: C-Arrays and Functions

An Array is a collection of variables having a similar data types but with unique subscript/index number.

Suppose we want to store numbers of 20 students of a class, then you have to take 20 different variables with 20 different unique names. This is complex process. C provides a facility to give a single unique name to a collection of variables.
This collection of variable is known as “Array”, values of array are called elements of the array and all the elements are stored at contiguous memory location means not in scattered in memory. It makes an execution speed fast.

Uses of Array in C programming: –

  • Saves time by declaring a different unique names of multiple variables.
  • Makes execution speed fast by providing contiguous memory location of variables.

How to declare an Array?
Both variable and Array declaration has a same Syntax to follow, but a little difference is of square brackets[ ] i.e. to be written with array name.
Syntax:               <data type><array name>[size of array];
Size of array defines the maximum number of elements going to be stored in array.
Eg:      int num[20];
This will store maximum 20 elements in num array. The index number of elements starts from 0. It is called lower bound of array and maximum or upper bound is always less then size of an array. The index value of an array is always an integer values like 0,1,2,3,4,5……n and so on. The first element of an array is always stored at 0th index and last element stores at size -1th index.
Eg:      int arr[5];
First element at arr[0]
Second element at arr[1] and so the last element at arr[4].
If we write array name with specified specific index in brackets, this will print the element stored at that particular index.
20 10 15 30 40
0        1        2        3       4
Index numbers
printf(“The element at First index is %d ”, arr[0]);  ——–     20
printf(“The element at 2th index is %d ”, arr[2]);     ——–     15
printf(“The element at last index is %d ”, arr[4]);    ——–     40
Note: the array name itself denotes the base address of an array. Means
printf(“The Base Address is %u ”, arr); and
printf(“The Base Address is %u ”, &arr[0]);
Both will print the same base address of an array.
The one biggest drawback of an array us that it is fixed memory location. Means maximum size of an array is compulsory to define at the declaration time and this leads to wastage of storage space if all this leads to wastage of storage space if all memory locations are not used and left unfilled.
Syntax:               int arr[ ];
It is an compilation error and not allowed because size of an array is not given which is compulsory to give. We can use an expression also for defining size but it can be written in brackets and it must not give the result in floating point. It must be an integer number only.
Eg:      int arr[20 * 2 + 4];   it is also valid.
More than one array can be declared in a single statement.
Eg:      int salary[20], marks[20];
Array Initialization: – Array can be initialized at the time of declaration or at the time of execution of a program.
                     At the declaration time: – by default the arrays elements are initialized by a garbage value not with zero.
Eg:           int arr[5] = {2, 3, 10, 20, 15};
int arr[] = {16, 20, 18, 19, 12, 1};
this is also valid syntax. In this case the size of an array is not compulsory to give. Compiler assumes the number of elements as the size of an array. Size of an array is 6.
At the time of Execution: – when we run a program, at that time also we can give the elements of an array. This can be done using loops.
Eg:           for(i = 0; i < 10; i++)
printf(“n Enter number : ”);
scanf(“%d”, &arr[i]);
Eg:                      Write a program to take input of 5 numbers in array and print it on screen?
Solution:             #include<stdio.h>
                           void main( )
int arr[5], i;
                                           for(i = 0; i < 5; i++)
                                                           printf(“n Enter number : ”);
                                                           scanf(“%d”, &arr[i]);
                                           for(i = 0; i < 5; i++)
                                                           printf(“n Number is %d”, arr[i]);

Difference between Normal variable and Array Variable: –

  • Simple variable stores a single value at a time while an array variable stores multiple values at a time.
  • In concept of simple variables, subscript no doesn`t matter, while in array all elements of array are identified by their unique subscript/index number.
  • Simple variable are stored scattered in a memory location, while array needs a contiguous memory location to get stored.
Types of an Array: – There are three types of array provided by C.
  1. One Dimensional Array
  2. Two Dimensional Array
  3. Multi Dimensional Array
  1. One Dimensional Array: –It contains only rows. So one loop is required to take input or print elements of an Array.
Syntax:    <data type><array name> [size];
Eg:           int arr[20];

2. Two Dimensional Array: – It consists rows and columns and so that is called matrix. The separate pair of square brackets is required.
                   Syntax:    <data type><array name> [for row][for column];
Eg:           int arr[2][3];
The first value specifies number of rows and second value specifies number of columns.
Three ways for Initialization of Two Dimensional Array:
  1. int arr[3][4] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
Note: while initializing an array no matter one or two Dimensional array, if values are entered less than size of an array then rest of the elements are automatically initialized by zero rather than error or garbage value.
Eg: int arr2[3][3] = {{1,2},{2,3,4}};
In this rest of the elements are initialized by zero automatically.
In two dimensional arrays, size of an array is to be calculated by multiplying the size of rows and columns.
For Eg. According to above example size of an array named arr2 is 3×3 = 9. It means total 9 elements can be stored in array.
  1. int arr[3][2] = {1,2,3,4,5,6};
this is also allowed in C.
  1. The third way of initializing the elements of an 2D array is by using Loops. The numbers of dimensions increases the number of loops for taking an input or for print elements on screen of an array.
   Eg:           for(i = 0; i<=2; i++)
                                   for(j = 0; j<=3; j++)
                                                   printf(“Enter Element : ”);
                                                   scanf(“%d”, arr[i][j]);
Eg:           Write a program to calculate average of 20 students belonging to 4 batches?
Solution:  #include<stdio.h>
                           void main( )
int arr[4][20];
                                           int i, j, sum = 0;
                                           for(i = 0; i < 4; i++)
                                                           for(j = 0; j<20; j++)
                                                                           printf(“n Enter Marks  : ”);
                                                                           scanf(“%d”, &arr[i][j]);
                                           for(i = 0; i < 4; i++)
                                                           for(j = 0; j<20; j++)
                                                                           sum = sum + arr[i][j];
                                           printf(“n Average is %d: , sum/20”);

3. Multi Dimensional Array:
More than two dimensions of array is also allowed in C programming. Multi dimensional array consists 3 or 4 separate square brackets according to requirement with array name.
Syntax:       <data type><array name>[ ][ ][ ];
Eg:              int arrr[3][4][5];
Total number of cells in this array is 60 cells.

C-Pointers: Previous                                                      Next: C-Arrays and Functions