C Programming
In this tutorial we will learn about array in C programming language.
Lets say we want to store score of 5 matches of a player. And lets say the scores are floating point numbers i.e., with decimal parts. So, one way of solving this is by declaring 5 float
type variables.
//score of 5 matches of a player
float score_1 = 10;
float score_2 = 12.6;
float score_3 = 20.1;
float score_4 = 19.8;
float score_5 = 22;
You can guess the problem by looking at the above approach.
We are creating too many variables that are logically connected. And imagine if we have to store score of 100 matches then following the above approach of creating a new float
variable for every match we will end up having 100 variables score_1
, score_2
and so on.
This is were array comes into picture.
An array is a fixed size sequenced collection of related data items and we refer this collection of items by a common name.
Arrays are derived type.
We can divide array into 3 types.
Following is a syntax of a one dimensional array.
type arrName[size];
Where, type
is any data type we want to use like int
, float
etc. And, arrName
is the name of the array variable.
Then we have the square brackets which contains an integer value size which tells us the total number of values we can store in the array variable.
Values stored in an array are referred as elements of the array.
For example, if we want to create an array named score of size 5 and type float then we will write the following code.
float score[5];
The above code instructs the computer to allocate memory space for 5 data values of type float and we can represent the score array as follows.
Note! Elements of the array are indexed meaning they are referred by an index number.
In the above image we can see that the first element of the array score is at index 0. The second element is at index 1. Similarly, the last element which is the 5th element is at index 4.
Array index starts from 0. So, if an array has 10 elements (size = 10) then the 1st element is at index 0 and the last element is at index (size - 1) i.e., 9.
To set a value for a given index we write the following code.
arrName[index] = value;
So, if we want to store the scores of the 5 matches in the array score
we can write the following code.
//save score of the 5 matches
score[0] = 10;
score[1] = 12.6;
score[2] = 20.1;
score[3] = 19.8;
score[4] = 22;
We can represent the values in the score array as follows.
We can combine the creation of array and assignment of value by writing the following.
type arrName[size] = {
value_1,
value_2,
value_3
};
So, if we want to store the score of 5 matches of a player then we can write the following.
//create the score array and assign value
float score[5] = {
10,
12.6,
20.1,
19.8,
22
};
In the above code we are creating an array variable of type float
and we are naming it score
. Then we are storing scores of the 5 matches.
There are two types of array initialisation.
In compile time initialisation we set the value of the array at the time of declaration.
The general syntax of compile time initialisation is given below.
type arrName[size] = { value1, value2, ... };
For example, if we want to create an array points
of type int
and want to store 3 values 10, 20 and 30 then, we will write the following code.
int points[3] = {10, 20, 30};
We can omit the size if we set all the values.
In the following example we are omitting the size and the compiler will still allocate enough space to store the 3 values.
int points[] = {10, 20, 30};
If we set the size but provide lesser value then rest of the array index location is filled with default value.
In the following example we have created an array points
of size 5 but have set only two values 10 and 20. So, the remaning index will be set to 0.
int points[5] = {10, 20};
//if we print the elements of the array points
//we will get 10, 20, 0, 0, 0
for (i = 0; i < 5; i++) {
printf("%d\n", points[i]);
}
Don't enter more elements in the array than the size of the array as it will give you error.
Following code will give error.
int arr[5] = {1, 2, 3, 4, 5, 6};
We can also initialise the array at run time by setting the elements of the array when the program is running.
In the following example we are creating an array num
of size 3 and setting the elements of the array at run time.
#include <stdio.h>
int main(void)
{
//variable
int num[5], i;
//user input
printf("Enter 5 integer values: ");
for (i = 0; i < 5; i++) {
scanf("%d", &num[i]);
}
//output
printf("Entered values:\n");
for (i = 0; i < 5; i++) {
printf("%d\n", num[i]);
}
printf("End of code\n");
return 0;
}
Output
Enter 5 integer values: 10 20 30 40 50
Entered values:
10
20
30
40
50
End of code
In the next tutorial we will learn about two dimensional arrays.
ADVERTISEMENT