An array stores a fixed number of values of the same type in a row in memory.
int marks[4] = {72, 95, 58, 88};
Indexes start at 0: marks[0].
Length: sizeof(marks) / sizeof(marks[0])
2D arrays: int grid[2][3];
C does not check array bounds. Reading marks[10] compiles, but it reads random memory: a classic source of bugs and security holes.
💡 Key Takeaways: C Arrays
Mastering C Arrays helps you write clean and reliable C code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
C Arrays Code Example
C
#include <stdio.h>
int main() {
int marks[] = {72, 95, 58, 88};
int n = sizeof(marks) / sizeof(marks[0]);
int sum = 0, max = marks[0];
for (int i = 0; i < n; i++) {
sum += marks[i];
if (marks[i] > max) max = marks[i];
}
printf("Count: %d, Sum: %d, Max: %d\n", n, sum, max);
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
printf("grid[1][2] = %d\n", grid[1][2]);
return 0;
}
Output
Count: 4, Sum: 313, Max: 95
grid[1][2] = 6
Run this example on your own computer. The "Get Started" lesson shows how to set it up.