So far, we’ve been working with single variables:
int age = 20;
int score = 85;
That works fine when you only need one value.
But what if you need to store:
5 test scores?
10 student IDs?
100 numbers?
Creating 100 separate variables would be messy.
This is where arrays come in.
1. What Is an Array?
An array is:
A collection of values of the same type stored next to each other in memory.
The important parts are:
Same data type
Stored in consecutive memory locations
Accessed using an index
2. Declaring an Array
Basic syntax:
data_type array_name[size];
Example:
int scores[5];
This creates:
An array named
scoresThat can hold 5 integers
Stored in continuous memory
Right now, the array is created but not initialized.
3. Initializing an Array
You can assign values when declaring:
int scores[5] = {85, 90, 78, 92, 88};
Now the array contains:
scores[0] = 85
scores[1] = 90
scores[2] = 78
scores[3] = 92
scores[4] = 88
4. Indexing (Very Important)
Array indexing starts at 0, not 1.
This is extremely important.
If the array has 5 elements:
int scores[5];
Valid indices are:
0, 1, 2, 3, 4
The last index is always:
size - 1
So for scores[5], the last index is 4.
5. Accessing Array Elements
You access elements using square brackets:
scores[0]
scores[1]
Example:
#include <stdio.h>
int main() {
int scores[3] = {10, 20, 30};
printf("%d\n", scores[0]);
printf("%d\n", scores[1]);
printf("%d\n", scores[2]);
return 0;
}
Output:
10
20
30
6. Modifying Array Elements
You can change values using indexing:
scores[1] = 50;
Now the array becomes:
10, 50, 30
Arrays are mutable (meaning their elements can change.)
7. Memory Layout (Why Arrays Matter)
Arrays are stored in contiguous memory.
If this is memory:
Address: 100 104 108 112 116
Value: 85 90 78 92 88
Each integer takes 4 bytes (on most systems).
So the addresses increase by 4.
This layout is important because:
It makes arrays efficient.
It allows pointer arithmetic later (we’ll get there).
8. Looping Through an Array
Arrays become powerful when combined with loops.
Instead of printing each element manually:
printf("%d\n", scores[0]);
printf("%d\n", scores[1]);
printf("%d\n", scores[2]);
We use a loop:
#include <stdio.h>
int main() {
int scores[3] = {10, 20, 30};
for (int i = 0; i < 3; i++) {
printf("%d\n", scores[i]);
}
return 0;
}
This prints all elements automatically.
9. Common Beginner Mistakes
❌ 1. Going Out of Bounds
scores[3] = 100;
If array size is 3, valid indices are 0–2.
Accessing scores[3] is undefined behavior.
C will not protect you.
It may:
Crash
Corrupt memory
Appear to work (but cause problems later)
Always stay within bounds.
❌ 2. Confusing Size With Last Index
If:
int a[5];
The last valid index is 4, not 5.
❌ 3. Forgetting Array Size in Loops
Always match your loop condition to the array size:
for (int i = 0; i < 5; i++)
Not:
i <= 5
10. Why Arrays Are Important
Arrays allow us to:
Store collections of data
Process bulk information
Prepare for strings
Understand memory structure
Work with pointers (next lesson)
Arrays are the foundation of more advanced data structures.
11. Practice Exercises
Create an array of 5 integers and print them using a loop.
Create an array of 4 numbers and calculate their sum.
Ask the user to input 3 numbers and store them in an array.
Find the largest number in an array.
Final Thoughts
Arrays teach you something important about C:
C gives you power.
But it does not protect you from mistakes.
You must:
Manage memory carefully
Respect array boundaries
Think about how data is stored
In the next lesson, we’ll go deeper into memory by learning about pointers.
And that’s where C starts becoming truly powerful.
About the author
- Heya! 👋 I love helping people, and one of the best ways I do this is by sharing what I learn and build. I started as a pharmacist at a tertiary hospital in Northern Ghana, where I saw how much technology could improve healthcare. That sparked my interest in digital health. I taught myself to code, then joined the ALX Software Engineering program, which became a turning point. I went on to complete a Master of Health Informatics at the University of Toronto. Today I am a software and AI engineer and a health informatician, and I practise remotely as a Digital Health Pharmacist. I combine clinical experience with technical skills to build tools that solve real problems in health and beyond, and I write here about what I build and what I learn.
Comments
Join the discussionNo comments yet. Be the first to comment.
C Programming
From today, I will be starting lessons on C programming in my ALX Software Engineering class and I look forward to sharing with you everything I learn through this series.
