One Dimensional Array in C

The array which is used to represent and store data in a linear form is called as Single or One Dimensional Array. To declare One Dimensional Array in C, the general syntax is as follows:

<data-type> <Array-name> [<Array-Size>];

The Array-Size must be an integer constant greater than zero and data-type can be any valid C data type; primary or user-defined. For example, to declare a 3 element one dimensional array called StudentsMarks of type double, we can use following statement:

double StudentMarks [3];

In order to initialize this array, we have two ways:
1. Initialize all elements at a time during declaration,
2. Initialize elements individually after declaration

The first method can be used to declare and assign values to all elements of 1D array as follows:

double StudentMarks [3] = {1.1, 2.2, 3.3};

This means element StudentMarks[0], which is the first element will be assigned value 1.1, StudentMarks[1] will be assigned value 2.2 and last element StudentMarks[2] will be assigned value 3.3. It should be noted that the number of values between braces { } can’t be larger than the number of elements that we declare for the array between square brackets [ ]. This means the following statement is invalid.

double StudentMarks [2] = {1.1, 2.2, 3.3};

However, if we omit the size of the array during declaration, an array just big enough to hold the values of initialization, is declared. Therefore, the following statement has same effect as the first valid statement above has.

double StudentMarks [] = {1.1, 2.2, 3.3};

The second method is to assign values to every element individually. Recall that to access any individual array element, the array name along with index within square braces is used, and it can be used the same way any other variable of same data type can be. Therefore, after declaration of StudentMarks array, we can initialize it as follows:

double StudentMarks [3];
StudentMarks[0] = 1.1;
StudentMarks[1] = 2.2;
StudentMarks[2] = 3.3;

The last statement above assigns element number 3 rd in the array a value of 3.3. Array with 2 nd index will be 3 rd i.e. last element because all arrays have 0 as the index of their first element. Following is the pictorial representation of the same array we discussed above:

Screenshot from 2020-07-20 02-46-49

Program 1 explains the declaration, assignment, access, usage and working of 1D array. It is followed by the output of the program.

#include <stdio.h>
void main ()
int n[ 10 ];
/* n is an array of 10 integers */
int i, j;
for ( i = 0; i < 10; i++ )
n[ i ] = i + 100;
/* set element at location i
to i + 100 */
/* output each array element’s value */
for (j = 0; j < 10; j++ )
printf(“Element[%d] = %d\n”,
j, n[j] );

Program 1: Program shows the usage and working of 1D array

Screenshot from 2020-07-20 02-48-29