AnyCompiler

Arrays

A C array has a fixed size set at creation with no built-in way to grow, and — unlike every other language on this site — it has no length property of its own. sizeof(array) / sizeof(array[0]) is the standard trick to recover an array's element count, and it only works while the array is still in scope as an array (not once it's decayed to a pointer, which the next lesson covers).

#include <stdio.h>

int main() {
    int numbers[5] = {4, 8, 15, 16, 23};
    int sum = 0;
    for (int i = 0; i < 5; i++) {
        sum += numbers[i];
    }
    printf("%d\n", sum);
    printf("Array size: %zu\n", sizeof(numbers) / sizeof(numbers[0]));
    return 0;
}
Output
66
Array size: 5

C also does no bounds checking: reading or writing past the end of an array doesn't raise an error — it just reads or corrupts whatever memory happens to be there. That's undefined behavior, and it's one of C's sharpest edges.

Next: pointers — the single idea the rest of this course builds on.

Practice exercise

Given int numbers[4] = {4, 8, 15, 16}, sum all its elements with a for loop and print the total.