Arrays in C: A Complete Beginner's Guide
An array in C is a fixed-size collection of values of the same data type stored in one continuous block of memory. Instead of declaring 100 separate variables, you declare one array and access each value through an index. Arrays make it easy to store, traverse, and process lists of numbers, characters, or any single type.
What is an array and why use one?
Imagine storing exam marks for 50 students. Without arrays you would need 50 variables, which is unmanageable. An array gives you one name and a numeric index to reach each element. In C, array indices start at 0, so the first element is at index 0 and the last is at index size minus one.
Every element of an array sits next to the previous one in memory. This is why arrays are fast: the computer can calculate the address of any element instantly using its index. The trade-off is that the size is fixed when you create the array, so you must decide capacity in advance.
Declaring and initializing a 1D array
To declare an array, you write the type, a name, and the size in square brackets. You can fill it with values immediately or assign them later by index.
#include <stdio.h>
int main(void) {
int marks[5]; // declares 5 integers (uninitialized)
int ages[3] = {18, 20, 22}; // declare and initialize
int zeros[4] = {0}; // every element becomes 0
marks[0] = 75; // assign by index
marks[1] = 88;
printf("%d\n", ages[1]); // prints 20
return 0;
}
If you provide initial values, you may omit the size and let the compiler count them: int days[] = {31, 28, 31}; creates an array of length 3. Listing fewer values than the size fills the rest with zero, which is handy for clearing an array.
A note on uninitialized arrays
If you declare int marks[5]; without initializing it, the elements hold garbage values, not zeros. Reading them before assigning gives unpredictable output. Always initialize, or assign every element before you use it. This single habit prevents many confusing bugs for beginners.
Traversing an array with loops
You almost always process arrays with a for loop. The loop counter doubles as the index, running from 0 up to size minus one.
#include <stdio.h>
int main(void) {
int marks[5] = {75, 88, 92, 60, 45};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += marks[i];
}
printf("Total = %d\n", sum); // 360
printf("Average = %d\n", sum / 5); // 72
return 0;
}
Notice the condition i < 5, not i <= 5. Because indices are 0 to 4, using <= would touch marks[5], which does not exist. That mistake is called an off-by-one error and is one of the most common pitfalls in C.
2D arrays: working with rows and columns
A two-dimensional array is an array of arrays, perfect for tables, grids, or matrices. You declare it with two sizes: rows and columns. Access an element with two indices.
#include <stdio.h>
int main(void) {
int grid[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// print every element using nested loops
for (int r = 0; r < 2; r++) {
for (int c = 0; c < 3; c++) {
printf("%d ", grid[r][c]);
}
printf("\n");
}
return 0;
}
The first index selects the row, the second selects the column. So grid[1][2] is 6. To traverse a 2D array you use nested loops: the outer loop walks the rows and the inner loop walks the columns. Think of a class register where rows are students and columns are subjects.
Arrays and functions
When you pass an array to a function, C does not copy the whole array. Instead it passes a reference to the first element, so the function works on the original data. This is efficient, but it also means changes inside the function affect the caller's array.
#include <stdio.h>
// the size must be passed separately
void doubleAll(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] = arr[i] * 2;
}
}
int main(void) {
int nums[4] = {1, 2, 3, 4};
doubleAll(nums, 4);
for (int i = 0; i < 4; i++) {
printf("%d ", nums[i]); // 2 4 6 8
}
return 0;
}
Because the function receives only a pointer, it cannot know the array length on its own. You must pass the size as a separate argument, as shown above. Forgetting this and guessing the length inside the function is a frequent source of bugs.
Strings are character arrays
In C, text is stored as an array of char ending with a special null character '\0'. That terminator tells functions where the string stops.
#include <stdio.h>
int main(void) {
char name[] = "Dhaka"; // 5 letters + '\0' = 6 bytes
printf("%s\n", name); // Dhaka
printf("%c\n", name[0]); // D
return 0;
}
Common pitfalls to avoid
Arrays are simple, but a few mistakes catch nearly every beginner. Watch for these carefully.
- Going out of bounds: C does not check whether your index is valid. Writing to
arr[10]in a 5-element array corrupts memory and may crash the program. Always keep indices between 0 and size minus one. - Off-by-one errors: using
<=instead of<in a loop, or forgetting that counting starts at 0. - Forgetting the null terminator: a character array used as a string needs room for
'\0', or string functions will read past the end. - Assuming arrays know their length inside functions: always pass the size along with the array.
- Using uninitialized elements: declare with initial values or assign before reading.
Quick practice exercise
Try writing a program that stores 5 numbers, then finds the largest. Start by assuming the first element is the maximum, then compare each remaining element against it.
#include <stdio.h>
int main(void) {
int data[5] = {23, 67, 12, 90, 45};
int max = data[0];
for (int i = 1; i < 5; i++) {
if (data[i] > max) {
max = data[i];
}
}
printf("Largest = %d\n", max); // 90
return 0;
}
Once this works, modify it to find the smallest value, or to count how many numbers are above the average. Small variations like these build real understanding far faster than only reading.
Frequently asked questions
Why do arrays in C start at index 0?
The index represents an offset from the start of the array. The first element sits at the very beginning, so its offset is 0. The address of element i is the base address plus i times the element size, which makes index 0 the natural starting point.
Can I change the size of an array after declaring it?
No. A standard C array has a fixed size set at declaration and cannot grow or shrink. If you need a resizable collection, you allocate memory dynamically with malloc and realloc from <stdlib.h>, which is a more advanced topic to learn next.
What happens if I read an index beyond the array size?
C does not stop you. You read whatever value happens to be in that memory location, which is unpredictable, or your program may crash with a segmentation fault. This is why checking your loop bounds carefully matters so much in C.
How is a 2D array stored in memory?
A 2D array is stored in row-major order, meaning the entire first row sits in memory, followed by the entire second row, and so on. It is really one continuous block. The two indices are just a convenient way to calculate the correct position.
Mastering arrays is a key step toward data structures, and strong C fundamentals open doors to software roles across Bangladesh; you can explore opportunities on Avian™ Career. Keep practicing with small programs, and the concepts will become second nature.
Categories
- Tutorial
- Announcement
- News