A program that handles the marks of sixty students could declare sixty variables, but it would be unbearably long and could not use a loop. An array stores all sixty marks under one name and lets a loop visit each in turn.

What Is an Array?

Definition: Array. A fixed-size collection of elements of the same data type, stored in adjacent memory locations and referred to by a single name.

Each element is identified by its position, called the index or subscript.

Declaring a One-Dimensional Array

The declaration int marks[5]; creates an array named marks with room for five integers. In C the first index is 0, so the elements are marks[0] to marks[4]. In general an array of size n has valid indices 0 to n − 1.

An array can be given values when it is declared: int marks[5] = {72, 85, 64, 91, 58};.

Reading and Writing Array Elements

An element is used exactly like an ordinary variable: marks[2] = 64; stores a value, and printf("%d", marks[2]); displays it. Because the index may be a variable, a single loop can process every element.

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

The loop runs from 0 while i < 5. Writing i <= 5 would refer to marks[5], which does not exist.

Array Bounds

C does not check whether an index is within range. Using an index outside 0 to n − 1 reads or overwrites memory that belongs to something else. The program may give wrong results or stop abruptly. Keeping every index within bounds is the programmer's responsibility.

Two-Dimensional Arrays

Definition: Two-dimensional array. An array whose elements are arranged in rows and columns and are identified by two indices, a row index and a column index.

The declaration int a[2][3]; creates a table of 2 rows and 3 columns. The element in row i and column j is a[i][j]. Both indices start at 0.

A two-dimensional array is processed with nested loops: the outer loop runs over the rows and the inner loop over the columns. Table 4.3 compares the two kinds of array.

Table 4.3: One-dimensional and two-dimensional arrays

Point One-dimensional Two-dimensional
Declaration int a[5]; int a[2][3];
Number of elements 5 2 × 3 = 6
Element a[i] a[i][j]
Processing One loop Two nested loops
Typical use A list of marks A table or a matrix

Example 4.3: Reading and displaying arrays

Problem: Read five marks into a one-dimensional array and display them in reverse order. Then display a 2 × 3 table row by row.

Solution:

  1. Read the marks with a loop from index 0 to 4.
  2. Display them with a loop from index 4 down to 0.
  3. Display the table with nested loops, printing a newline after each row.
#include <stdio.h>

int main(void)
{
    int marks[5];
    int t[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int i, j;

    printf("Enter 5 marks: ");
    for (i = 0; i < 5; i++)
    {
        scanf("%d", &marks[i]);
    }
    printf("\nIn reverse: ");
    for (i = 4; i >= 0; i--)
    {
        printf("%d ", marks[i]);
    }
    printf("\nTable:\n");
    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 3; j++)
        {
            printf("%d ", t[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Input:

72 85 64 91 58

Output:

Enter 5 marks:
In reverse: 58 91 64 85 72
Table:
1 2 3
4 5 6

Result: The same array is traversed forwards for input and backwards for output simply by changing the loop. The two-dimensional array needs one loop for the rows and another for the columns.

Common mistakes

  • Using indices 1 to n instead of 0 to n − 1.
  • Writing i <= n in the loop condition, which goes one element beyond the end of the array.
  • Assuming that C will report an index that is out of range.

Key takeaways

  • An array holds a fixed number of elements of one type under a single name.
  • Indices start at 0; an array of size n has indices 0 to n − 1.
  • C does not check array bounds.
  • A two-dimensional array uses two indices and is processed with nested loops.

Practice questions

  1. What are the valid indices of the array int x[10]?
  2. How many elements does int m[4][5] contain?
  3. Which element is in the second row and third column of the array a?

Answers to the practice questions

  1. 0 to 9.
  2. 4 × 5 = 20 elements.
  3. a[1][2].

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV