One-Dimensional Arrays in C: Concepts, Declaration, and Initialization

So far, every variable we’ve used has held exactly one value. An array lets you store multiple values of the same type under a single name, accessed by position. This is Post 3 of the Unit II series — Post 2 covered break, continue, and goto.

Why Arrays?

Imagine storing the marks of 50 students. Without arrays, you’d need 50 separate variables (marks1, marks2, …, marks50) — unmanageable, and impossible to loop over. An array solves this with one declaration and index-based access.

Declaration and Initialization

int marks[50];                          // declaration: array of 50 ints, uninitialized (garbage values)
int scores[5] = {90, 85, 77, 60, 95};    // declaration + initialization
int values[] = {1, 2, 3, 4};             // size inferred from the initializer list (4)
int zeros[10] = {0};                      // first element 0, REST are automatically zeroed too
Zero-based indexing: Array elements are numbered starting from 0, not 1. In int scores[5], valid indexes are scores[0] through scores[4] — there is no scores[5]. This trips up almost every beginner at least once.
Out-of-bounds access: undefined behavior, not a safe error: Unlike some languages, C does NOT check array bounds at runtime. Writing to scores[10] on a 5-element array will not necessarily crash — it may silently corrupt some other variable’s memory, work “fine” for a while, and then fail unpredictably much later. Always double-check your loop conditions (< size, not <= size) when working with arrays.

Accessing and Modifying Elements

#include <stdio.h>

int main() {
    int scores[5] = {90, 85, 77, 60, 95};

    printf("First score: %d\n", scores[0]);
    printf("Third score: %d\n", scores[2]);

    scores[1] = 88;   // modify an element
    printf("Updated second score: %d\n", scores[1]);

    return 0;
}

Sample Output

First score: 90
Third score: 77
Updated second score: 88

Reading and Printing an Entire Array with a Loop

#include <stdio.h>

int main() {
    int n;
    int arr[100];

    printf("How many numbers? ");
    scanf("%d", &n);

    printf("Enter %d numbers:\n", n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("You entered: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

Sample Output

How many numbers? 4
Enter 4 numbers:
12 45 7 23
You entered: 12 45 7 23

Common Array Operations: Sum and Average

#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int n = 5;
    int sum = 0;

    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }

    float average = (float) sum / n;

    printf("Sum: %d\n", sum);
    printf("Average: %.2f\n", average);

    return 0;
}

Sample Output

Sum: 150
Average: 30.00
Related reading on this site: For worked examples of finding the largest/smallest element, linear search, and second-largest-without-sorting on a 1-D array, see the earlier lab-exam posts on this blog — they build directly on the concepts here.

Arrays and Memory

An array’s elements are stored in contiguous (back-to-back) memory locations. This is why arr[i] works so efficiently — the compiler just computes address_of(arr[0]) + i * size_of(element) to find any element instantly, without scanning through the array.

Summary

  • An array stores multiple values of the same type under one name, accessed by a zero-based index.
  • C does not perform bounds checking — going out of range is undefined behavior, not a caught error.
  • Arrays are stored in contiguous memory, making indexed access very fast.
  • Looping with for (i = 0; i < n; i++) is the standard pattern for processing every element.

Further Reading

Next in this series: two-dimensional arrays.

Leave a Comment