Learn how to declare, initialize, and access C arrays, with clear memory diagrams.
int a[5]; declares an array of 5a[0] to a[4]int a[] = {1,2,3};sizeof(a)/sizeof(a[0]) for element countint score1 = 80; int score2 = 95; int score3 = 72; // A hundred students? This does not scale!
int score[100]; score[0] = 80; score[1] = 95; // A for loop processes all elements
int a[5] = {10, 20, 30, 40, 50}; printf("%d\n", a[0]); // → 10 printf("%d\n", a[4]); // → 50
int a[5] in 5 gives the number of elements; a[5] does not exist in the array! [5]: specify the number of elements → reserve memory for five elements [ ]: specify a position → only 0 through 4 are valid a[0]through a[4] only . Distinguish the declared size, 5, from the final index, 4. a[5]or a[100] can compile successfully , sometimes without warnings. At runtime they can corrupt data, crash with a segmentation fault, or appear to work until a bug surfaces later. i < 5 restricts indices to 0–4. i <= 5 instead accesses a[5], causing an off-by-one error. = { ... }, any omitted elements are automatically initialized to zero.int a[5] = {1, 2, 3, 4, 5}; // Specify every element int b[5] = {1, 2}; // → {1, 2, 0, 0, 0}: remaining elements are zero int c[5] = {0}; // → All elements zero: a common pattern int d[] = {1, 2, 3}; // Omitted size: inferred as three elements int e[5]; // No initializer: indeterminate values for this local array
| Syntax | Result | Key point |
|---|---|---|
{1,2,3,4,5} | 1, 2, 3, 4, 5 | Every element specified |
{1,2} | 1, 2, 0, 0, 0 | Remaining elements zero |
{0} | 0, 0, 0, 0, 0 | All-zero initialization |
int d[] = {…} | Count inferred | Size can be omitted |
int e[5]; | Indeterminate values | Initialize before use |
=, a local automatic array's contents are indeterminate . Before reading elements, initialize them with {0} or assign values explicitly. | Name | Type | Value |
|---|
i < N keeps indices in range. int a[5] = {10, 20, 30, 40, 50}; int sum = 0; // Initialize the accumulator to zero for (int i = 0; i < 5; i++) { sum += a[i]; } printf("%d\n", sum); // → 150
int max = a[0]; // Start from the first element (important) for (int i = 1; i < 5; i++) { if (a[i] > max) max = a[i]; } printf("%d\n", max); // → 50
max = 0 fails when the array contains only negative values . Initialize it using max = a[0], the value of the first element . int target = 30, found = -1; for (int i = 0; i < 5; i++) { if (a[i] == target) { found = i; break; } } printf("%d\n", found); // → 2 (the index where the value was found)
i < N(<= would go out of bounds). 80+65+92+78+88 = 403; the true average is 403 ÷ 5 = 80.6. int / int → int).
double avg = (double)sum / 5; to convert one operand to double before division. printf("Average = %.1f\n", avg); (%.1f for one decimal place). doubleand (double)sumUse a cast to obtain the true average, 80.6 Check your understanding of this lesson!
int a[5]; allocate?a[5] gives you 5 elements, from a[0] through a[4].
C array indices always start at 0. In a[5], a[0] is the first element.
int a[3] = {1, 2};, what is the value of a[2]?When an array is partially initialized, the leftover elements are zero-initialized. So a[2] is 0.