An array stores a fixed number of values of the same type in contiguous memory, accessed by a zero-based index. This section covers declaring and initializing arrays, traversing them, two-dimensional and multidimensional arrays, variable length arrays (VLAs), passing arrays to functions, and the everyday operations of searching, inserting, and reversing.
Arrays do not know their length.
C does not store or check an array's size. You must track the length yourself and keep every index in the range 0 to n - 1.
Path
Learning Path
Work through the stages in order, or jump to a topic from the index below (9 tutorials).
A quick taste of arrays. Compile with gcc file.c -std=c11 -Wall -o out.
Example 1 — Average of an array
Declare, initialize, loop, and compute.
C
#include <stdio.h>
int main(void) {
int scores[] = {85, 90, 78, 92, 88};
int n = sizeof scores / sizeof scores[0];
int sum = 0;
for (int i = 0; i < n; i++) {
sum += scores[i];
}
printf("n=%d sum=%d average=%.1f\n", n, sum, (double)sum / n);
return 0;
}
Output
n=5 sum=433 average=86.6
How It Works
The compiler infers the size 5 from the initializer.
sizeof scores / sizeof scores[0] computes the element count.
The cast keeps the average fractional: 433 / 5 = 86.6.
Index
C Arrays Topics
Every guide in this section with a one-line summary (9 tutorials).