Nearby lessons

52 of 124

C - Array Introduction

An array stores many values of the same type under one name. Learn why arrays exist, how they are laid out in memory, and the zero-based indexing rule that trips up every beginner.

The Problem Arrays Solve

Suppose you need the marks of five students. Without arrays you would declare five separate variables — and the code gets worse with every student you add:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 /* The painful way */
6 int mark1 = 85, mark2 = 90, mark3 = 78, mark4 = 92, mark5 = 88;
7 int total = mark1 + mark2 + mark3 + mark4 + mark5;
8 printf("Total: %d\n", total);
9 
10 /* Now imagine 500 students... */
11 return 0;
12}
Output
Total: 433

The Same Job with an Array

One name, one loop, and the code no longer grows with the number of students:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int marks[5] = {85, 90, 78, 92, 88};
6 int i, total = 0;
7 
8 for (i = 0; i < 5; i++)
9 total += marks[i];
10 
11 printf("Total: %d\n", total);
12 printf("Average: %.2f\n", total / 5.0);
13 return 0;
14}
Output
Total: 433
Average: 86.60

What an Array Really Is

An array is a block of memory holding a fixed number of elements, all of the same data type, stored one immediately after another.

Index01234
Value8590789288
Address10001004100810121016

Each int takes 4 bytes, so the addresses step by 4. Because the elements are evenly spaced, C can jump straight to any element with one multiplication — which is why array access is so fast.

In simple words: an array is a row of identical letterboxes numbered from 0. Give C the number and it walks directly to that box.

Why Indexing Starts at 0

The index is not a position — it is an offset from the start of the block. The first element is 0 boxes away from the beginning, so its index is 0.

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[5] = {10, 20, 30, 40, 50};
6 
7 printf("nums[0] = %d (first)\n", nums[0]);
8 printf("nums[4] = %d (last)\n", nums[4]);
9 
10 /* Addresses prove the elements are contiguous */
11 printf("Address of nums[0]: %p\n", (void *) &nums[0]);
12 printf("Address of nums[1]: %p\n", (void *) &nums[1]);
13 return 0;
14}
Output
nums[0] = 10  (first)
nums[4] = 50  (last)
Address of nums[0]: 000000000061FE00
Address of nums[1]: 000000000061FE04

The Off-By-One Trap

An array of size 5 has valid indexes 0 to 4. There is no nums[5], and C will not stop you from using it:

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int nums[5] = {10, 20, 30, 40, 50};
6 
7 printf("Valid : nums[4] = %d\n", nums[4]);
8 printf("INVALID : nums[5] = %d\n", nums[5]); /* garbage - out of bounds */
9 return 0;
10}
Output
Valid   : nums[4] = 50
INVALID : nums[5] = 4200880

C Does Not Check Bounds

This is C's most dangerous behaviour. Reading nums[5] or nums[1000] compiles without a single warning. It reads whatever bytes happen to be there — another variable, or memory that is not yours. Writing out of bounds silently corrupts your program and may crash it much later, far from the real bug.

Languages like Java and Python throw an exception here. C hands you the responsibility, so always loop with i < size, never i <= size.

Advantages and Limitations

AdvantagesLimitations
One name for many valuesFixed size — cannot grow at runtime
Instant access by indexAll elements must be the same type
Works naturally with loopsNo bounds checking
Cache-friendly and fastInserting or deleting in the middle means shifting elements
Easy to pass to functionsCannot be assigned or compared with = or ==
Trainer's Note: When you genuinely need a resizable collection in C, you allocate memory yourself with malloc and realloc. That is a later topic — master fixed arrays first.
📝 Key Takeaways
  • An array is a fixed-size collection of same-type values.
  • Indexing starts at 0, so the last index is size - 1.
  • Elements are stored side by side in contiguous memory.
  • The size must be known at compile time (in classic C).
  • C never checks whether your index is in range.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4