Understanding Arrays in C: A Complete Guide for Beginners
See Array Types
What is an Array?
Imagine you have a row of boxes lined up, and each box can store one value — like your favorite toys in boxes.
An array in C is just like those boxes but for storing values of the same type (like all numbers or all characters).
Instead of giving each box a different name, you give the whole row a single name and use numbers (called indexes) to open each box.
How to Use Arrays? Step by Step
- Declare an array: Tell the computer what type of data you want to store and how many boxes you need.
int numbers[5]; // Creates an array named 'numbers' to store 5 integers
- Initialize the array: You can put values inside the boxes when you declare or later.
int numbers[5] = {10, 20, 30, 40, 50};
Or fill them one by one:
numbers[0] = 10;
numbers[1] = 20;
// and so on...
- Access elements: Use the index to get or change the value inside a box.
printf("%d\n", numbers[2]); // Prints the 3rd element (indexes start at 0)
- Loop through arrays: Usually, we use loops to go through all boxes easily.
for (int i = 0; i < 5; i++) {
printf("%d\n", numbers[i]);
}
Important Things to Know About Arrays
- Indexes start at 0: The first box is
array[0], the second array[1], and so on.
- Fixed size: Once you create an array with a certain size, it cannot grow or shrink during the program.
- Same data type: All elements in the array must be of the same type (all int, all char, etc.).
- Contiguous memory: Elements are stored one after another in memory, making access very fast.
Common Mistakes to Avoid When Using Arrays
1. Accessing Out of Bounds:
Never use an index less than 0 or greater than or equal to the size of the array.
Example mistake: numbers[5] = 100; is wrong if array size is 5 (max index is 4).
This leads to undefined behavior and possible program crashes.
2. Not Initializing Arrays:
If you don’t assign values, the array elements contain garbage (random data). Always initialize before use.
Example mistake: Using int arr[3]; and printing values before assigning.
3. Forgetting Index Starts at 0:
Beginners sometimes try to access array[1] thinking it’s the first element. It’s actually the second.
Always remember: first element is array[0].
4. Mixing Data Types:
You cannot mix types inside an array. For example, you can’t have some elements as int and others as float in the same array.
5. Ignoring Array Size When Looping:
Make sure your loop runs from 0 to size-1 to avoid accessing invalid elements.
Wrong loop: for (int i = 0; i <= 5; i++) for an array of size 5.
Correct loop: for (int i = 0; i < 5; i++).
Examples for Practice
Example 1: Declare, initialize and print an array
int scores[4] = {85, 90, 78, 92};
for (int i = 0; i < 4; i++) {
printf("Score %d: %d\n", i+1, scores[i]);
}
Example 2: Change an element in the array
scores[2] = 80; // Change 3rd score from 78 to 80
printf("Updated Score 3: %d\n", scores[2]);
Example 3: What happens if you access outside the array?
printf("%d\n", scores[4]); // Invalid! Array size is 4, max index 3
This might print garbage or cause a crash. Avoid!
Summary
- Arrays store multiple values of the same type in order.
- Indexes always start at 0.
- Declare size before use; size cannot change later.
- Use loops to efficiently access array elements.
- Avoid common mistakes like out-of-bound access and uninitialized values.
Further Learning
For more detailed explanations and examples, visit: