Lesson 10·C Programming

Arrays in C

Contiguous memory collections of homogeneous elements with index access

What is an Array?

An Array is a collection of homogeneous (same data type) elements stored in contiguous memory locations under a single unique identifier name.

Individual elements in an array are accessed using integer indices called subscripts, starting at index 0 up to n - 1. Because of this, arrays are also referred to as 'subscripted variables.'

1. Single (One) Dimensional Array

Used to store and represent linear sequences of values in memory:

C Example
#include <stdio.h>

void main() {
    int a[3], i;

    printf("Enter three numbers: ");
    for (i = 0; i < 3; i++) {
        scanf("%d", &a[i]);  // read array element
    }

    printf("\nNumbers are: ");
    for (i = 0; i < 3; i++) {
        printf("%d  ", a[i]);  // print array element
    }
}
Program Output
Enter three numbers: 9 4 6

Numbers are: 9  4  6
Single Dimensional Array Memory Allocation Diagram
Sequential memory allocation for a 1D array showing contiguous memory byte addresses.

2. Two Dimensional Array (Matrix)

Used to represent and store data in tabular or matrix form with rows and columns:

C Example
#include <stdio.h>

void main() {
    int a[3][3], i, j;

    printf("Enter 3 x 3 matrix elements:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%d", &a[i][j]);  // read row and column
        }
    }

    printf("\nMatrix is:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d\t", a[i][j]);  // print matrix row
        }
        printf("\n");
    }
}
Program Output
Enter 3 x 3 matrix elements:
3 4 5 6 7 2 1 2 3

Matrix is:
3   4   5
6   7   2
1   2   3
Two Dimensional Array Memory Allocation Diagram
Row-major memory organization of a 3x3 2D array in C.