1D Arrays in C language – How to declare, initialize and access elements

An array is a collection of data that holds a fixed number of values of the same data type.

These are declared under the same variable name and are accessed using it. In our previous tutorial of Data Types, we saw that array belongs to secondary data types which means that it is derived from the primary data type. If we declare an integer array, all the values in the array have to be an integer. Similarly, for a character array, all the elements of the array are characters and same goes for double and every other data type. That means it comprises data belonging to the primary data type in bulk.

If we did not have an array it would have been very difficult for a programmer to create so many variables and then organize and manage it. An array helps in reducing the complexity of a program. With the help of an array, we can store any number of data under one variable name which is pretty easy to manage. The data stored in an array are organized in a very ordered fashion. Every data that is stored in an array is known as an ‘array element’. The position where the data is stored is known as an ‘index position’.  Each array element is given a certain index number.

Note that since the index position starts form 0, the index limit is always one less than the actual size of the array.
The following diagram illustrates the array elements and the index positions where they are stored.

array-elements

In the diagram, the characters ‘C’,’O’,’D’,’I’,’N’,’G’,’E’,’E’,’K’ are separate elements stored in the array in a ordered manner starting from the 0 index position till the 8th index. In the following section, we shall learn more about the arrays.


Declaring an array

Like the primary datatypes, the arrays also need to be declared to let the compiler know what type of data is to be stored in the given array. An array can be of type int ,  float, double  or char . The character arrays in C are known as ‘strings’. Here is the how a character array is declared:

int num[50];

The int specifies that the data stored in the array will be of integer type.
num is the variable name under which all the data is stored.
[50] refers to the size of the array. This is a static memory allocation, that is, we are allocating the array a size equal to

This is a static memory allocation, that is, we are allocating the array of size equal to 50 integers or the array can store 50 integer elements. The [] bracket specifies the size of the array and it also indicates the ‘dimension’ of the array. In this tutorial we will deal with single dimensional or 1-D arrays only.


Initializing and storing data in an array

An array initialization can take place during the declaration of the array as well. It means we give certain values to the array or store the elements of the array during declaration. Here are a few examples of array initialization:

//Declaring and initialising a character array
char name[9]={'C','O','D','I','N','G','E','E','K'};

//Declaring and initialising an integer array
int num[]={1,2,3,4,5}; 

//Declaring and initialising a float array
float n[]={0.1,0.2,0.3,0.4,0.5}; 

Here we see that the size of the array might or might not be stated during declaration when the array has been initialized.

Another way of storing data in an array is by user input. We generally use a for loop to store data inside an array(although it can be done without it). The loop runs from 0 which is the first index position to the (size of the array – 1) and the array elements are scanned in the array in a sequential order. Here is the code snippet that shows how this task is performed.

int a[5],i;
for(i = 0; i < 5; i++){ 
    scanf("%d",&a[i]);
}

Accessing and Reading the array

The array is easily accessed with the help of the index numbers. Note that if the programmer does not initialize the array or input the data in the array, the array will display garbage values.In the above figure, if we need to access the element ‘G’. We just need to write the array name followed by the ‘subscript’. Eg:

name[5]

The [5] is the ‘subscript’ of the array. Subscript is the number in the parentheses following the array name. It indicates the position of the array element in the array. To read all the elements of an array we again use a for loop. The for loop runs from 0 which is the first index position to the size of the array-1 and the printf() function displays all the elements in an array. Here is how it is done:

int i;
for(i = 0; i < 5; i++){
    printf("%d",name[i]);
}

Array program for input and output

Here is a simple program of array, which adds two arrays and stores the result in another array. One array has already been initialized and the other one will have data input by the user.


#include <stdio.h>
int main()
{
    //we initialize the first array and the second array will have user input values
    int a[3] = {1,34,5};//first array initialization
    int b[3], c[3], i;
    
    printf("Enter values in the 3 element 1D array:\n");
    for(i=0 ;i<3 ;i++){ //loop for values input
        scanf("%d",&b[i]);
        c[i] = a[i] + b[i];//summing up the values of the two arrays
    }
    
    printf("Resulting array after sum is");
    //displaying the array elements after summing up
    for(i=0 ;i<3 ;i++){
        printf("%d  \n",c[i]);
    }
    return 0;
}


Output:-
Enter values in the 3 element 1D array
13
9
0

Resulting array after sum is :
15
43
5

Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.

Do not forget to share and Subscribe.

Keep Learning… Happy Learning.. 🙂

Recommended -