What is an Array?
An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int, float, and char. So an integer array can only hold integer values and cannot hold values other than integer. When we declare array, it allocates contiguous memory location for storing values whereas 2 or 3 variables of same data-type can have random locations. So this is the most important difference between a variable and an array.
Types of Arrays:
- One dimension array (Also known as 1-D array).
- Two dimension array (Also known as 2-D array).
- Multi-dimension array.
One dimension array
A llist of items can be given one variable name using only one subscript and such a variable is called a single-subscripted variable or a one dimensional array.
Declaration of One Dimensional Arrays
Syntax: data_type array_name[width];
Example: int roll[8];
In our example, int specifies the type if the variable, roll specifies the name of the variable and the value in bracket [8] is new for newbie. The bracket ([ ]) tells compiler that it is an array and number mention in the bracket specifies that how many elements (values in any array is called elements) it can store. This number is called dimension of array.
So, with respect to our example we have declared an array of integer type and named it “roll” which can store roll numbers of 8 students. You can see memory arrangement of above declared array in the following image:
Example: int roll[8];
In our example, int specifies the type if the variable, roll specifies the name of the variable and the value in bracket [8] is new for newbie. The bracket ([ ]) tells compiler that it is an array and number mention in the bracket specifies that how many elements (values in any array is called elements) it can store. This number is called dimension of array.
So, with respect to our example we have declared an array of integer type and named it “roll” which can store roll numbers of 8 students. You can see memory arrangement of above declared array in the following image:
C Array Assignment and Initialization
We can initialize and assign values to the arrays in the same way as we do with variable. We can assign value to an array at the time of declaration or during runtime. Let’s look at each approach.Syntax: data_type array_name[size]={list of values};
Example:
int arr[5]={1,2,3,4,5};
int arr[]={1,2,3,4,5};
In our above array example we have declared an integer array and named it “arr” which can hold 5 elements, we are also initializing arrays in the same time.
Both statements in our example are valid method to declare and initialize single dimension array. In our first example we mention the size (5) of an array and assigned it values in curly brace, separating element’s value by comma (,). But in second example we left the size field blank but we provided its element’s value. When we only give element values without providing size of an array then C compiler automatically assumes its size from given element values.
There is one more method to initialize array C programming; in this method we can assign values to individual element of an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5],i;
clrscr();
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;
printf("Value in array arr[0] : %d\n",arr[0]);
printf("Value in array arr[1] : %d\n",arr[1]);
printf("Value in array arr[2] : %d\n",arr[2]);
printf("Value in array arr[3] : %d\n",arr[3]);
printf("Value in array arr[4] : %d\n",arr[4]);
printf("\n");
for(i=0;i<5;i++)
{
printf("Value in array arr[%d] : %d\n",i,arr[i]);
}
getch();
}
#include<conio.h>
void main()
{
int arr[5],i;
clrscr();
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;
printf("Value in array arr[0] : %d\n",arr[0]);
printf("Value in array arr[1] : %d\n",arr[1]);
printf("Value in array arr[2] : %d\n",arr[2]);
printf("Value in array arr[3] : %d\n",arr[3]);
printf("Value in array arr[4] : %d\n",arr[4]);
printf("\n");
for(i=0;i<5;i++)
{
printf("Value in array arr[%d] : %d\n",i,arr[i]);
}
getch();
}
Two dimension array
So far we have discussed the array variables that can store a list of values. Sometimes there will be a situations where a table of values will have to be stored. Because it is a 2D array so its structure will be different from one
dimension array. The 2D array is also known as Matrix or Table, it is
an array of array.Declaration of 2D array
Syntax: data_type array_name[row_size][column_size];Example: int arr[3][3];
So the above example declares a 2D array of integer type. This integer array has been named arr and it can hold up to 9 elements (3 rows x 3 columns).
#include <stdio.h>
main(){
int a[3][2];
int i,j;
for(i = 0;i<3;i++){
for(j=0;j<2 ;j++) {
a[i][j]=2;
}
}
for(i = 0;i<3;i++){
for(j=0;j<2;j++) {
printf("value in array %d\n",a[i][j]);
}
}
for(i = 0;i<3;i++){
for(j=0;j<2;j++){
printf("value in array %d and address is %8u\n", a[i][j],&a[i][j]);
}
}
}
Multi dimension array
'C' allows arrays of three or more dimensions. The exct limit is determined by the compiler. The general form of a multidimensional array is
type array_name [s1[s2] . .. [sm];
where sm is the size of the mth dimension. Some examples are
int survey[3][5][12];
float table[5][4][5][3];
survey is a three-dimensional array declared to contain 180 integer type elements.
table is a four-dimensional array containing 300 elements of floating point type.
Example
#include <stdio.h> #include <conio.h> const int num_rows = 7; const int num_columns = 5; void main() { int box[num_rows][num_columns]; int row, column; for(row = 0; row < num_rows; row++) for(column = 0; column < num_columns; column++) box[row][column] = column + (row * num_columns); for(row = 0; row < num_rows; row++) { for(column = 0; column < num_columns; column++) { printf("%4d", box[row][column]); } printf("\n"); } getch(); }
output
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34



