Tsallaka zuwa abun ciki
Justice Tech School

Arrays

An array holds a fixed number of values of the same type. Indexes start at 0. C does not check bounds: reading or writing outside the array is undefined behaviour and a common source of bugs. Get the number of items with sizeof(arr) / sizeof(arr[0]).

Example
int nums[5] = {5, 2, 9, 1, 7};
int count = sizeof(nums) / sizeof(nums[0]);
printf("%d items, first %d\n", count, nums[0]);
int sum = 0;
for (int i = 0; i < count; i++) sum += nums[i];
printf("sum %d\n", sum);
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("%d\n", grid[1][2]);

Result

5 items, first 5
sum 24
6

Exercise

The first index of an array is...

nums[]

Quiz Test

Does C check array bounds?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.