One name for many values
An array gives you numbered boxes under a single name. The numbering starts at zero, and understanding why prevents the most common crash in C.
After this lesson
You should be able to
- Declare and initialise an array and access its elements by subscript.
- Explain why the last valid subscript is size minus one.
- Walk an array with a for loop without running off either end.
Fifty marks, one name
Storing fifty students' marks in fifty variables is unmanageable — you cannot loop over m1, m2, m3. An array gives you one name and a number: marks[0] through marks[49]. Now a loop can visit every one.
The elements sit next to each other in memory, all of the same type. That is why the size must be known when you declare it, and why every element is the same kind of thing.
int marks[5]; /* five ints, uninitialised */
int scores[5] = {72, 45, 91, 38, 66};
int zeros[5] = {0}; /* first is 0, rest filled with 0 */
marks[0] = 87; /* first element */
marks[4] = 55; /* last element */Why counting starts at zero
The subscript is not a position, it is an offset — how far from the start. The first element is zero steps from the start, so it is marks[0]. An array of five therefore ends at marks[4], and marks[5] is one past the end.
C does not check subscripts. Writing marks[5] on a five-element array compiles and runs, quietly reading or writing whatever memory sits next door. The bug often appears far away, in an unrelated variable, which is what makes it so hard to find.
Walking an array
A for loop and an array fit together naturally: the counter is the subscript. Start at 0, continue while i < n, step by one. Every element, exactly once.
Use a named constant for the size instead of scattering 5 through the code. When the size changes you edit one line, and the loop bound can never disagree with the declaration.
#include <stdio.h>
#define SIZE 5
int main(void)
{
int marks[SIZE] = {72, 45, 91, 38, 66};
int total = 0;
for (int i = 0; i < SIZE; i++)
total += marks[i];
printf("Total %d, average %.2f\n", total, (double) total / SIZE);
return 0;
}Try it yourself
Read seven daily temperatures into an array, then print how many days were above the week's average.
Need a hint?
You cannot count above-average days until you know the average, so you need two passes over the array.
Check the worked solution
Two loops are genuinely required here — the comparison cannot begin until every value has contributed to the average, so no single pass can do it. Note the cast on the average: without it, 7 ints divided by 7 would truncate and the comparison would be against the wrong number.
#include <stdio.h>
#define DAYS 7
int main(void)
{
int temp[DAYS];
int total = 0, above = 0;
double average;
for (int i = 0; i < DAYS; i++) {
printf("Day %d: ", i + 1);
scanf("%d", &temp[i]);
total += temp[i];
}
average = (double) total / DAYS;
for (int i = 0; i < DAYS; i++)
if (temp[i] > average)
above++;
printf("Average %.2f, above on %d days\n", average, above);
return 0;
}Quick check
For int a[5]; what happens when you write a[5] = 1;?
Why this lesson exists
Syllabus mapping
Declaring and Referencing Arrays · Array Subscripts · Using for Loops for Sequential Access
Maps to course outcomes CO3, CO5.