You are on page 1of 2

It is a collection of similar type of data which can be either of int, float, double,

char (String), etc. All the data types must be same. For example, we can't have an
array in which some of the data are integer and some are float.
Why Array?
Consider a scenario where you need to find out the average of 100 integer numbers
entered by user. In C, you have two ways to do this: 1) Define 100 variables with
int data type and then perform 100 scanf() operations to store the entered values in
the variables and then at last calculate the average of them. 2) Have a single integer
array to store all the values, loop the array to store all the entered values in array
and later calculate the average.

datatype array_name [ array_size ] ;

For example, take an array of integers 'n'.

int n[10];

n[ ] is used to denote an array 'n'. It means that 'n' is an array.

So, int n[10] means that 'n' is an array of 6 integers. Here, 6 is the size of the array
i.e. there are 10 elements in the array 'n'.

Access Array Elements


You can access elements of an array by indices.

Suppose you declared an array num. The first element is num[0], the second
element is num[1] and so on.

Few keynotes:
Arrays have 0 as the first index, not 1. In this example, num[0] is the first element.
If the size of an array is n, to access the last element, the n-1 index is used. In this
example, num[4]
Suppose the starting address of num[0] is 1120d. Then, the address of the num[1]
will be 1124d. Similarly, the address of num[2] will be 1128d and so on.
This is because the size of a float is 4 bytes.

Assigning Values to Array


The first way of assigning values to the elements of an array is by doing so at the
time of its declaration.

int n[ ]={ 2,4,8 };

And the second method is declaring the array first and then assigning values to its
elements.
int n[3];
n[0] = 2;
n[1] = 4;
n[2] = 8;

You might also like