What an array is and why you need one
An array in C is a container that holds multiple values of the same type in a single variable. Instead of creating ten separate variables to store ten numbers, you create one array that holds all ten. C accesses each value by its position in the array, starting from zero.
Arrays matter because they let you work with groups of related data without writing repetitive code. If you need to find the largest number in a list, calculate an average, or sort values, an array makes that task straightforward. Without arrays, you would write the same logic over and over for each individual variable.
Key Takeaways
- Declare an array by writing the data type, the array name, and the number of elements in square brackets: int numbers[5];
- Array positions start at zero, so in a five-element array, valid positions are 0, 1, 2, 3, and 4.
- Assign values to array positions using the position number in square brackets: numbers[0] = 10;
- You can declare and initialize an array at the same time: int numbers[5] = {10, 20, 30, 40, 50};
- Use a loop to access or modify multiple array elements without writing each one individually.
Declaring an array with a fixed size
To create an array, you write the data type, then the array name, then the number of elements inside square brackets. Here is an array that holds five integers:
int numbers[5];
This line creates space in memory for five integers. The array is named numbers, and it can hold exactly five values. You cannot add a sixth value to this array later — the size is fixed when you declare it.
You can create arrays of any data type: float prices[10]; creates an array of ten decimal numbers, and char letters[26]; creates an array of twenty-six characters. The principle is the same: type, name, size in brackets.
Storing values in an array using index positions
Each position in an array has a number called an index. The first position is always index 0, the second is index 1, and so on. In a five-element array, the last position is index 4.
To put a value into a specific position, write the array name, then the index in square brackets, then the value:
numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50;
Now the array numbers contains five values. If you try to access numbers[5], the program will not stop you from writing the code, but it will access memory that does not belong to the array, which causes unpredictable behavior. Always stay within the bounds you declared.
Initializing an array when you declare it
Instead of declaring an array and then filling it one value at a time, you can declare and initialize it in a single line. Put the values inside curly braces, separated by commas:
int numbers[5] = {10, 20, 30, 40, 50};
This creates the array and fills all five positions in one step. You can also let C count the elements for you by leaving the brackets empty:
int numbers[] = {10, 20, 30, 40, 50};
C will see five values and automatically make the array size 5. This is useful when you know your values ahead of time and do not want to count them manually. If you initialize only some positions, the rest are filled with zeros: int numbers[5] = {10, 20}; creates an array where positions 0 and 1 hold 10 and 20, and positions 2, 3, and 4 hold 0.
Using loops to work with multiple array elements
The real power of arrays appears when you use a loop to access many elements at once. A for loop can step through each position and perform the same action on every element:
for (int i = 0; i < 5; i++) { printf("%d\n", numbers[i]); }
This loop starts at position 0, prints the value at that position, then moves to position 1, and continues until it reaches position 4. The variable i holds the current index, so numbers[i] means "the value at position i".
You can modify array values inside a loop just as easily. This loop doubles every value in the array:
for (int i = 0; i < 5; i++) { numbers[i] = numbers[i] * 2; }
Loops are essential because they scale. Whether your array holds 5 elements or 500, the same loop code works without change. Without loops, you would have to write 500 separate lines to modify 500 values.
Common mistakes when working with arrays
The most frequent error is forgetting that array positions start at zero. If you declare int numbers[5];, the valid positions are 0, 1, 2, 3, and 4 — not 1 through 5. Trying to access numbers[5] reads or writes memory outside the array.
Another mistake is declaring an array size that is too small and then trying to add more elements. Arrays have a fixed size in C. If you need to store more values than you planned, you must declare a larger array or use a different approach like dynamic memory allocation with malloc().
A third common issue is using an uninitialized array. If you declare int numbers[5]; without assigning values, the array contains garbage data — whatever was in that memory location before. Always initialize your arrays or assign values before reading from them.
Frequently Asked Questions
Can I change the size of an array after I create it?
No. Arrays in C have a fixed size that you set when you declare them. If you need a different size, you must declare a new array. For situations where you do not know the size ahead of time, you can use dynamic memory allocation with malloc(), which lets you request memory at runtime.
What happens if I access an array position that does not exist?
C does not stop you. It will read or write whatever is in that memory location, which causes unpredictable results or crashes. Always make sure your loop condition and index values stay within the bounds you declared — 0 to size minus 1.
Can I have an array of arrays?
Yes. A two-dimensional array is declared like int grid[3][4];, which creates a 3-by-4 table. You access elements with two indices: grid[0][1] means row 0, column 1. Multi-dimensional arrays work the same way as single arrays but require nested loops to access all elements.
Do I have to initialize every element when I declare an array?
No. You can declare an array without initializing it, but then it contains garbage data. You can also initialize only some elements: int numbers[5] = {10, 20}; fills positions 0 and 1, and C fills the rest with zeros. It is safer to initialize all elements or assign values before reading.