Lesson 12
Arrays - Part 1
An array is a collection of variables of the same data type stored in contiguous memory locations. This guide covers array declaration, indexing, initialization, memory representation, traversal, and one-dimensional, two-dimensional, and multidimensional arrays.
C Track
Save progress as you learn.
44 lessons
Lesson content
Arrays in C Programming: A Complete Beginner to Advanced Guide
If you've ever needed to store multiple values of the same type in your program, Arrays in C Programming are one of the first and most important concepts you'll learn. Arrays make your code cleaner, faster, and easier to manage by allowing you to store many values under a single variable name.
In this guide, you'll learn everything from the basics of arrays to advanced concepts with practical examples.
What are Arrays in C Programming?
An array is a collection of variables of the same data type stored in contiguous (continuous) memory locations.
Instead of creating many separate variables like this:
int mark1 = 90;
int mark2 = 85;
int mark3 = 76;
int mark4 = 95;
int mark5 = 88;You can simply write:
int marks[5];Now all five marks are stored inside one variable named marks. Think of an array like a row of lockers, where each locker has an index, starting from 0.
Why Do Arrays Exist?
Imagine a classroom with 100 students. Without arrays, you would need 100 separate variables (student1, student2, student3, and so on), which would be difficult to manage.
Arrays solve this problem by storing all values under one variable name:
int marks[100];Now every student's mark can be accessed using its index: marks[0], marks[1], marks[2], and so on up to marks[99].
This makes programs:
- Easier to write
- Easier to read
- Easier to maintain
- More efficient
Real-World Use Cases
Arrays are used almost everywhere in programming, including:
- Storing student marks
- Employee IDs
- Temperature readings
- Image processing, where every digital image is stored as a huge array of pixels
- Game development, for player scores, enemy positions, inventory items, and map tiles
- Banking applications, for account balances, daily transactions, and customer IDs
Prerequisites
Before learning Arrays in C Programming, you should already know:
- Variables
- Data types
- Input and Output (printf() and scanf())
- Operators
- Basic loops (for, while)
- Conditional statements (if, switch)
If you're comfortable with these topics, you're ready to learn arrays.
Core Concepts
1. Declaring an Array
Syntax
datatype arrayName[size];Example
int marks[5];| Part | Meaning |
|---|---|
| int | Data type |
| marks | Array name |
| 5 | Number of elements |
2. Array Size
The size tells the compiler how many elements the array can store.
int numbers[10];This array stores 10 integers, with indexes ranging from 0 to 9. Notice that the last index is always size - 1.
3. Array Index
Arrays always begin with index 0.
int numbers[5];Accessing values: numbers[0], numbers[1], numbers[2], numbers[3], numbers[4].
Trying to access numbers[5] is out of bounds and leads to undefined behavior.
4. Initializing Arrays
Method 1: Initialize During Declaration
int numbers[5] = {10,20,30,40,50};Method 2: Compiler Counts Automatically
int numbers[] = {10,20,30,40,50};Compiler creates an array of size 5.
Method 3: Partial Initialization
int numbers[5] = {10,20};Result: the elements become 10, 20, 0, 0, 0. Remaining elements become 0.
5. Accessing Array Elements
Each element is accessed using its index.
printf("%d", marks[3]);Output: the fourth element.
6. Modifying Array Elements
You can change any element.
marks[2] = 100;Only the third element changes.
7. Memory Representation
One of the most important concepts. Suppose int numbers[5]; and assume the first address is 1000. Since one integer occupies 4 bytes, memory becomes:
| Index | Address |
|---|---|
| 0 | 1000 |
| 1 | 1004 |
| 2 | 1008 |
| 3 | 1012 |
| 4 | 1016 |
This continuous memory arrangement is what makes arrays very fast.
8. Traversing an Array
Traversing means visiting every element. The most common way is using a for loop.
for(i = 0; i < size; i++)
{
// Access array element
}The loop starts from index 0 and continues until the last valid index.
9. One-Dimensional Arrays
A one-dimensional array stores data in a single row.
int values[5];Applications: student marks, monthly sales, temperatures, scores.
10. Two-Dimensional Arrays
A two-dimensional array stores data in rows and columns, much like a table or spreadsheet.
int matrix[3][4];This creates a table with 3 rows and 4 columns.
Common uses: matrices, game boards, seating arrangements, image pixels.
11. Multidimensional Arrays
C also supports arrays with more than two dimensions.
int cube[3][4][2];This can represent 3D graphics, scientific data, and simulation models. Although powerful, multidimensional arrays consume more memory and are typically used in advanced applications.
12. Advantages of Arrays
- Store many values using one variable
- Easy to process with loops
- Faster access using indexes
- Contiguous memory improves performance
- Foundation for advanced data structures like stacks, queues, and matrices
13. Limitations of Arrays
- Fixed size after declaration
- Can only store one data type
- Inserting or deleting elements is expensive
- Out-of-bounds access causes undefined behavior
- Large arrays can consume significant memory
14. Arrays vs Individual Variables
| Feature | Individual Variables | Arrays |
|---|---|---|
| Number of variables | Many | One |
| Easy to manage | No | Yes |
| Loop support | No | Yes |
| Memory organization | Separate variables | Contiguous |
| Scalability | Poor | Excellent |
Key Concepts Learned So Far
By now, you should understand:
- What an array is
- Why arrays are needed
- How arrays are declared
- Array initialization methods
- Zero-based indexing
- Memory layout
- Traversing arrays with loops
- One-dimensional and multidimensional arrays
- Advantages and limitations of arrays