C Workshop · Arrays
Foundation C

Arrays

An array is a row of same-type boxes. The first index is 0. The last is n-1.

What it is

An array is a row of boxes of the same type, sitting next to each other in memory. The first box is index 0, the last is n-1. You pick a box with a[i].

Approach

  1. Know the length n.
  2. Store values in a[0] .. a[n-1].
  3. Use a for-loop to read or scan them.

Where this is used in the real world

Diagram

1

a[0]=1

first

2

a[1]=8

next

3

a[2]=3

4

a[3]=5

last

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2int main(void) {
3    int a[4] = {1, 8, 3, 5};
4    printf("%d\n", a[1]);
5    return 0;
6}
What this line does
Press Run algorithm. The highlight will move through the C code.
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. First index is:

Zero-based.

2. a[1] in {1,8,3,5} is:

Second slot.

3. Last index of length 4 is:

n-1.

4. a[4] on this array is:

Do not go to n.

5. A 2-D array would use:

Rows and columns.