Arrays

Arrays in C Language | Simple Tutorial

Hi guys.. Welcome to the Digital Chalkie Educational Blog. Today I’m going to discuss “Arrays in C programming”. This article belongs to our  ” C Programming Tutorial Series”. You can read other articles by click here.

Definition :-  An array is a group (or collection) of the same data types. For example, an int array holds the elements of int types while a float array holds the elements of float types.

Reason to we need arrays.

Imagine that you have to calculate the summation of hundred numbers that entered by a user. You can define 100 variables with the int data type. Then perform a hundred scanf()  operations and store the values for each variable and get the summation. But we know it is complicated to write a program like that. Also, it is time wastage.

We know that using a cupboard is easier than using hundred of small bags. In the C we use the same method. We define a small group called “Array” to store variables.  For the above scenario, we can define an integer array to save the values and use them.

How to declare an Array.

We use a code following to declare an array.  We can declare arrays of any data type as int, float, double, char.

data_type  arrayName[size

int numbers[5];  /* An integer array of 5 elements */
char name[10];  /* An array of characters for 10 elements */

How to access an element of Array

To access the elements of an Array, we use the index ( array subscript) of the element. If we think array is a complex of small rooms, the room number of the element that we considering called “array subscript or index”. The important thing is that ” Array Subscripts are beginning from the zero.” It means the first array subscript is zero.  We use the following code to access the array.

arrayName[index]   :  Usually we use arr[n-1] to access nth element of an array. where n is an integer number.

int numbers[20]; //Define a new array of 20 elements
numbers[0] /* first element of array numbers*/
numbers[19] /* last (20th) element of array numbers*/

Input Data into the array

 Suppose that we have a size 4 array. To input data to this array we use simple for loop. Here we are repeating the array from 0 to 3 because the size of the array is 4. Inside the loop, we are displaying a message to the user to enter the values. All the input values are stored in the corresponding array elements using the scanf() function.

int num[4]
for (x=0; x<4;x++)  // x++ is equal to x=x+1
{ 
    printf("Enter a number \n");
    scanf("%d", &num[x]); // Here "&num[x] is the address to corrresponding array subscript
}

Reading out data from an Array

As above we also use a for loop to read out the data from arrays.

To print
for (x=0; x<4;x++)
{
    printf("num[%d]\n", num[x]);
}
Usually we use %d when we work with int variable. In this case we work with arrays and 
we use [%d] to represent the arrray
Example to calculation
 for (x=0; x<4;x++)
{
 sum = sum+num[x];
}

Initializing an Array

When we work with variables we initialize them to prevent the printing garbage values. Also, we can initialize the arrays as following way to protecting from garbage values.

int arr[5] = {1, 2, 3, 4 ,5};

OK guys.. This is the end of our tutorial lesson about Arrays in C Language.  If there any questions, suggestions or opinions please comment them below in the comment section. Good Bye All..

Bonomi Tharinda
Bonomi Tharinda is the Developer of the BMaxx Blog. He is a skilled web developer and graphic designer. In addition, he is an undergraduate student of a representative University in Sri Lanka. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.Here are his social accounts and you can follow him through them.