Elementary Overview
OSC.007 introduces C arrays: a fixed number of elements of the same type stored consecutively. Arrays help a program process measurements, sensor samples or scores without declaring a new variable for every value. C indexes begin at zero; an array containing five elements has valid positions 0 through 4.
The sequence follows OSC.006 functions, OSC.005 loops and OSC.002 fundamental types. Students will declare arrays, compute their element count and prevent invalid indexing.
Programiz, C Arrays, an original instructional video covering declarations, initialization, indexing and loop traversal Original publisher video.
Declaration and initialization
The declaration int readings[5]; reserves space for five int objects. An uninitialized automatic array contains indeterminate values, so do not read its elements before assigning them. In contrast, int readings[5] = {12, 18, 25, 30, 15}; initializes every position. A partial initializer such as int values[4] = {2, 4}; initializes the two remaining elements to zero.
Compile and test a complete C11 program
#include <stdio.h>
#include <stddef.h>
int main(void)
{
int readings[] = {12, 18, 25, 30, 15};
size_t count = sizeof readings / sizeof readings[0];
int total = 0;
for (size_t i = 0; i < count; ++i) {
total += readings[i];
}
printf("count=%zu total=%d average=%.1f\n",
count, total, (double)total / count);
return 0;
}
The program prints count=5 total=100 average=20.0. The example was compiled and run using GCC with -std=c11 -Wall -Wextra -Wpedantic -Werror and address/undefined-behavior sanitizers. The result describes the tested example, not proof that every C program is safe.
Why sizeof needs context
sizeof readings gives the entire array size in bytes; sizeof readings[0] gives one element’s size. Their quotient produces five here, independent of how many bytes the implementation assigns to int. Perform the calculation where the actual array object is visible.
A function declared void inspect(int data[], size_t length) receives a pointer parameter rather than a complete array object. Its sizeof data therefore cannot recover the caller’s element count. Pass length separately, as explained by cppreference’s C array reference. Pointer behavior receives fuller treatment in a later lesson.
Bounds are a safety requirement
readings[5] does not designate the sixth valid element. Accessing beyond the array bounds causes undefined behavior; a program might appear to work, crash or corrupt data. Use conditions such as i < count, not i <= count. The SEI CERT ARR30-C rule specifically addresses invalid array subscripts.
Neso Academy, Arrays in C: Solved Problem 1, an original worked tutorial showing array processing Original publisher video.
Practice and knowledge check
- Change the array to
{3, 6, 9}, recompile and predict the count, total and average before running. - Explain why the final valid index is
count - 1when count is positive. - Explain why
sizeof datainside an ordinary array-parameter function cannot determine the caller’s length.
For an earlier review of comparison operators and loop conditions, revisit OSC.004 control flow. The OSC.001 compiler lesson provides build prerequisites. Reference: GCC warning options.
Elementary Conclusion
C arrays organize multiple same-type values, but programmers remain responsible for initialization, element counts and valid indices. Combine arrays with loops and functions, preserve lengths at interfaces, and test boundary conditions before trusting output.
Editor’s Note: The original cinematic-anime cover illustrates a fictional C workstation. It is not an actual debugger screenshot. Both videos come from the identified educational publishers. The program was locally compiled and run; YouTube playback and mobile visual appearance have not been independently verified.

BitcoinVersus.Tech is volunteer-run. An optional 5,000-satoshi contribution supports independent lessons. Confirm receiving address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb and payment amount before sending an irreversible transaction.
We volunteer daily to ensure the information on BitcoinVersus.Tech is Verifiably True. Learning materials are educational, not a substitute for production software review.

Leave a Reply