typestar

Arrays in C

Fixed-size arrays: initializing and iterating.

int sum_array(void) {
    int scores[5] = {90, 82, 77, 95, 88};
    int total = 0;
    for (int i = 0; i < 5; i++)
        total += scores[i];

    int doubled[5];
    for (int i = 0; i < 5; i++)
        doubled[i] = scores[i] * 2;
    return total + doubled[0];
}

How it works

  1. A braced initializer fills the array.
  2. One loop sums it; another fills a second array.
  3. Indexing with [i] reads and writes elements.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
224
Tokens
94
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 63 seconds.

Type this snippet

Step 1 of 5 in Arrays & strings, step 22 of 35 in Language basics.

← Previous Next →

Arrays in other languages