Theory
Sixty variables, or one idea
The marks program must remember all sixty students' marks AT ONCE (you cannot find the topper while forgetting each mark as the next arrives).
The horror-movie version: int m1, m2, m3, ... m60; and sixty scanf lines.
The C version: one name, sixty numbered boxes:
int marks[60];
That is an array, and after loops, it is the second biggest idea in this subject. It also hides C's most famous surprise: the numbering starts at ZERO.
Theory
A train coach
An array is a railway coach: ONE coach name (S4), many identical berths, each with a number. You never say "the berth named Ramesh"; you say "S4, berth 23". Same shape berths (same data type), consecutive (side by side in memory), reachable instantly by number. C's one quirk: the first berth is numbered 0, and every later lesson in this unit leans on that fact.
Theory
Declaration and initialization
- Declare:
int marks[5];reserves five int boxes, size fixed for the array's lifetime. - Initialize:
int marks[5] = {70, 55, 90, 62, 78}; - Partial:
int marks[5] = {70, 55};fills the rest with zeros. - Let C count:
int marks[] = {70, 55, 90};makes a size-3 array.
Indexing: marks[0] is the FIRST element, marks[4] the LAST of five. The index runs 0 to n-1, always. And marks[i] with a for loop is the marriage this unit is about:
for (i = 0; i < 5; i++) printf("%d\n", marks[i]);
Theory
Character arrays: text in C
Text is just an array of chars:
char name[10] = "Riya";
Memory holds: R i y a `\0` and five spare boxes. That \0 (the null terminator, ASCII 0) is invisible but essential: it marks WHERE THE TEXT ENDS, since the array itself is bigger than the word. Every string function you will meet (strlen, printf %s...) walks the array until it hits \0.
Count carefully: "Riya" needs at least 5 boxes, four letters plus the terminator.
Quiz
For int marks[5], which indexes are valid?
- 0 to 4
- 1 to 5
- 0 to 5
- 1 to 4
Show the answer
0 to 4
Five boxes, numbered from zero: 0, 1, 2, 3, 4. Writing marks[5] steps OUTSIDE the array, and C does not stop you: it compiles, runs, and quietly corrupts whatever lives next door (undefined behaviour). The n-boxes-means-0-to-n-1 rule is the most-tested array fact in existence.
Think first
Read the boxes
Given int marks[5] = {70, 55, 90, 62, 78}; work out in your head: what are marks[0], marks[2], and marks[4]? And what is dangerous about marks[5]?
Show the answer
marks[0] = 70 (first box), marks[2] = 90 (third box), marks[4] = 78 (fifth and last). marks[5] would be the SIXTH box of a five-box array: out of bounds, no compiler error, garbage read or neighbour-corrupting write at runtime. If you answered 55 for marks[2], you counted from one, the exact habit this unit must break.
Quiz
char city[10] = "Surat"; How many boxes does the text actually occupy, and what sits right after the letter t?
- 6 boxes: S-u-r-a-t plus the terminator '\0'
- 5 boxes: exactly the five letters
- 10 boxes: the whole array is the text
- 6 boxes: five letters plus a space character
Show the answer
6 boxes: S-u-r-a-t plus the terminator '\0'
String literals bring an invisible guest: the null terminator '\0' after the last letter, so "Surat" occupies six boxes inside the ten-box array. The remaining four are spare. It is '\0' (ASCII 0), not a space (ASCII 32), and forgetting this +1 is the classic char-array sizing error.
Watch out
Where marks (and programs) leak
Counting from one: marks[1] is the SECOND element; burn in zero-start. Out of bounds: valid indexes end at n-1; marks[n] compiles fine and corrupts silently, C trusts you completely. Forgetting '\0' space: char name[4] = "Riya" has no room for the terminator, and string functions will run off the end. Fixed size: an array cannot grow later; declare for the maximum you need.
Theory
The unit's road from here
One more secret for later: the array's NAME (marks) is actually the ADDRESS of box zero, which is why scanf("%s", name) needed no &, and which becomes the whole story of pointers, three lessons ahead. Next, though: making arrays WORK, searching for a roll number and sorting the toppers list, the operations exams ask every single year.
Summary
Key takeaways
- An array: one name, n same-type boxes, consecutive in memory, size fixed.
- Indexing starts at ZERO: valid indexes are 0 to n-1; marks[0] is first.
- Initialize fully, partially (rest become 0), or let C count with {}.
- marks[i] + a for loop replaces dozens of variables.
- char arrays store text with an invisible '\0' terminator: "Riya" needs 5 boxes.
- Out-of-bounds access compiles silently and corrupts, C never checks.
- Memory hook: a train coach, berths numbered from zero.