WeCodeAcademy

C Arrays

An array stores several values of one type. Indexes start at 0. C does not check bounds, so reading past the end is a bug that can crash the program.

ExampleC
#include <stdio.h>

int main(void) {
  int nums[5] = {10, 20, 30, 40, 50};
  printf("%d\n", nums[0]);
  nums[1] = 99;

  int total = 0;
  int count = sizeof(nums) / sizeof(nums[0]);
  for (int i = 0; i < count; i++) {
    total += nums[i];
  }
  printf("count=%d total=%d\n", count, total);

  int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
  printf("%d\n", grid[1][2]);
  return 0;
}
Output
10
count=5 total=229
6
Quick check

What does C do if you read past the end of an array?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.