Three sessions fit in three variables: first, second, third. A week of them does not, and a loop cannot step through separately named variables at all. An array solves both problems. It is one variable that holds a fixed number of elements of the same type, numbered from 0, and a loop counter can choose an element by its number. The catch, and the main subject of this lesson, is that C trusts you to use only numbers that exist. It does not check.
Predict and run
#include <stdio.h>
int main(void) {
int minutes[3] = {25, 10, 15};
for (int i = 0; i < 3; i++) {
printf("%d\n", minutes[i]);
}
return 0;
}Check your prediction
25
10
15int minutes[3] creates three int elements side by side in memory. The index in square brackets chooses one: minutes[0] is the first, minutes[1] the second and minutes[2] the third. Change the middle value to 20 and predict which line changes. The index is a distance from the start of the array, which is why the first element is at distance 0.
Valid indexes stop one before the size
An array of size 3 has exactly the indexes 0, 1 and 2. Index 3 is not a fourth slot. It is the position just past the end, where some other data, or nothing usable at all, may live. The loop above continues while i is less than 3, so it stops after 2. A loop with "at most 3" instead would make one more pass and read minutes[3].
int minutes[3] = {25, 10, 15};
for (int i = 0; i <= 3; i++) {
printf("%d\n", minutes[i]);
}C does not check array bounds. The read of minutes[3] is undefined behavior: the standard says nothing about what happens. It might print a leftover number, it might crash, and with optimization the compiler is entitled to assume that your program never does this, and to transform the loop on that assumption in ways that do not match the source at all. "It printed something reasonable when I tried it" is not evidence that the code is correct. Do not run this version to see what happens; reason about the bound instead. A safe loop over n elements starts at 0 and continues while the index is less than n.
Initialization and size
The values in braces fill the elements in order. If you give fewer values than the size, the remaining elements are set to zero. If you omit the size, the compiler counts the values for you. sizeof gives the size of an object in bytes, and dividing the whole array's size by one element's size gives the number of elements, whatever the size of an int on your machine.
#include <stdio.h>
int main(void) {
int week[7] = {30, 45};
int planned[] = {20, 20, 25};
printf("%d %d %d\n", week[0], week[1], week[6]);
printf("%zu\n", sizeof planned / sizeof planned[0]);
return 0;
}30 45 0
3%zu is the placeholder for a size, of type size_t. That sizeof trick only works on the array itself. The next lesson shows that inside a function receiving an array, it silently gives the wrong answer.
An uninitialized local array, int week[7]; with no braces, holds indeterminate values, exactly like an uninitialized int. Writing = {0} is the short way to start every element at zero.
Capacity is not count
A report rarely fills its storage exactly. You might reserve room for 8 sessions and have logged 3 so far. Keep two numbers apart:
- Capacity is how many elements the array has. It is fixed when the array is declared.
- Count is how many of them currently hold real data. It changes as you add sessions.
Loops over the data use the count, not the capacity, so unused slots never reach the report. Adding a session means storing it at index count, the first unused slot, and then increasing count. That only works while count is less than the capacity; Day 6 turns that check into a function.
Practice
Your turn
Start with int minutes[6] = {25, 40, 10}; and int count = 3;. Add a fourth session of 20 minutes by storing it at index count and increasing count. Then print every logged session, numbered from 1 for the reader, and the total:
Session 1: 25
Session 2: 40
Session 3: 10
Session 4: 20
Total: 95 over 4 sessions#include <stdio.h>
int main(void) {
int minutes[6] = {25, 40, 10};
int count = 3;
// Write your solution here.
return 0;
}Hint 1: the idea
Indexes start at 0 but people count from 1. Loop over indexes 0 to count - 1 and print i + 1 as the session number. Accumulate the values, not the indexes.
Hint 2: a first step
Write minutes[count] = 20; and then count++;. Print count to check it is 4 before you write the loop.
Show one solution
#include <stdio.h>
int main(void) {
int minutes[6] = {25, 40, 10};
int count = 3;
minutes[count] = 20;
count++;
int total = 0;
for (int i = 0; i < count; i++) {
printf("Session %d: %d\n", i + 1, minutes[i]);
total += minutes[i];
}
printf("Total: %d over %d sessions\n", total, count);
return 0;
}Session 1: 25
Session 2: 40
Session 3: 10
Session 4: 20
Total: 95 over 4 sessionsThe new session went into index 3, the first unused slot, and the loop stopped at count, so the two zero elements at the end never reached the report. The array has capacity 6, so the store was safe; with a full array it would not be. Next you will move this loop into a function and find out why the function also needs to be told the count.
Completion is saved in this browser only. It does not measure mastery.