HowToProgramC : Lesson 14

Back Home Next


Our programming toolkit is almost complete but still a very important component is missing. We are going to discuss this component i.e. Arrays in this lesson. Array is a special data-type. If we have a collection of data of same type like the ages of 100 students, arrays can be used. Arrays are data structure in which identical data types are stored. In C language, every array has a data type i.e. name and size. Data type can be any valid data type. The rules of variable naming convention apply to array names. The size of the array tells how many elements are there in the array. The size of the array should be a precise number. The arrays occupy the memory depending upon their size and have contiguous area of memory. We can access the arrays using the array index.


The declaration of arrays is as follows:

data_type array_name [size] ;

for example:

int ages[10];

Let's consider an array int C[10]; This is an array of integer and has a name ’C'. It has a size ten which depicts that the array ‘C’ can contain ten elements of int data type. In the memory, the array occupies the contiguous area, in this case it will occupy forty bytes (one int = 4 bytes). The elements of the array are manipulated using the index. In C language, the index of array starts from zero and is one less than array's size. Index of array is also called subscript.Arrays may be declared with simple variables in a single line.

int i, age [10];
int height [10], length [10] ;

To access array, we can’t use the whole array at a time. We access arrays element by element. An index (subscript) may be used to access the first element of the array. In this case, to access first element we write like age[0]. To access the 5th element, we will write age[4] and so on. Using the index mechanism, we can use the array elements as simple variables. We can use them anywhere, where we can use a simple variable i.e. in assignment statements, expressions etc.

Initialization of Arrays

There are many ways to initialize an array. Don't use the default initialization of arrays. Compiler may assign some value to each declared array. Always initialize the array in such a manner that the process is clear. We can initialize an array using a 'loop' while assigning some value.

int i, age [10];
for ( i = 0; i < 10 ; i++ )
age[i] = 0;

With the help of this simple loop, we have initialized all the elements of array age to zero. In the loop condition, we have used the condition i < 10, where the size of the array is ten. As we know, the array index is one less than the size of the array. Here we are using i as the index of array and its values are from 0 to 9. We can also initialize the array at the time of declaration as:

int age [10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

The above statement creates an array age of integers and initializes all the elements with zero. We can use any value to initialize the array by using any other number instead of zero. However, generally, zero is used to initialize the integer variables. We can do it by using the following shortcut.

int age [10] = { 0 };

The above statement has also initialized all the elements of the array to zero. We have different ways of initializing the arrays. Initialization through the use of loop is a better choice. If the size of the array gets larger, it is tedious to initialize at the declaration time. Consider the following statement:

int age [ ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

Here we have not mentioned the size of the array. The compiler is quite intelligent as it detects the initialization list which consists of ten 0’s. Therefore, it creates an array of 10 integers and initializes all the elements with zero. The index of the arrays starts from the index 0 and is up to one less than the size of the array. So if the size of the array is ten, the index will be from 0 to 9. Similarly, if the size of the array is 253, the index will be from 0 to 252.

Back Home Next

© All rights Reserved.