When we’re talking data structures, arrays are one of the most common you’ll come across. This is true no matter what programming language you’re working with. Arrays are common because they’re one of the simplest ways to store elements that share a data type. Today, we’re going to look at how arrays in C work, how they’re implemented, and their pros and cons.
What are Arrays in C?
We can define an array as an element collection with elements that are contiguously stored. In simple terms, contiguous means that the elements are stored adjacently in the memory, with no gaps. This is generally done so that accessing each element is easier. For most intents and purposes, you can think of contiguous as having a similar meaning to continuous. Click here for a primer on the different kinds of data structures.
How Are Arrays in C Different from Other Languages?
Arrays in C have some key differences from other languages. The main ones are:
- Static – arrays in C aren’t dynamic, meaning their size is fixed at the point of creation. This is unlike languages like Python, where arrays can be resized dynamically.
- Bounds must be checked – Arrays need to be manually checked so that all their indices stay within bounds. Some languages do this automatically, such as Java and Python.
- Memory allocation – Memory in C is generally allocated on the stack, whereas C# and Java allocate memory on the heap, which is not automatic.
How to Use Arrays in C
Now, it’s time to move on to how to use arrays in C. We’re going to cover declaring arrays, initializing them, and accessing them.
Declaration of an Array
The syntax for declaring an array is relatively simple. All we have to do is specify the data type, array name, and size. For example:
type name[size];
The type can be a variety of things, such as character, float, or integer.
To illustrate, if we wanted to create an array called “ExampleArray” with 10 integer elements, we would write:
int ExampleArray[10];
In C, indices begin at 0, so the indices for these elements would range from 0 to 9.
Initializing an Array in C
To initialize an array at the point of declaration, we can provide an initializer list. This sounds complicated, but it really isn’t. By using curved brackets, or braces, we can provide the element values without having to explicitly specify the array size. This can be done as such:
int ExampleArray[] = {2, 4, 6, 8, 10};
This will produce a 5-element array like before, with values 2, 4, 6, 8, and 10. The compiler figures out the array size automatically from the list, so we don’t need to specify it ourselves.
Accessing the Elements
A very common operation to perform on an array is accessing its elements. This is generally done to check their value, change them, or add or remove elements adjacently. It’s basically as simple as initializing an array and is done by using the index of the element we want. For example:
int ExampleArray[5] = {2, 4, 6, 8, 10};
int firstNumber = ExampleArray[0];
Here, we use the same array as before, the “firstNumber” specifier, and the index of the element we want to access. In this case, this is the first element.
Modifying the Elements
As briefly mentioned before, arrays in C are fixed and their size cannot be easily altered, but this is technically possible (more on this later). An easier operation, however, is changing the element at a given index. The following code shows this.
#include <stdio.h>
int main() {
int ExampleArray[] = {2, 4, 6, 8, 10};
int length = sizeof(ExampleArray) / sizeof(ExampleArray[0]);
ExampleArray[2] = 10;
ExampleArray[4] = ExampleArray[0] + ExampleArray[1];
for (int i = 0; i < length; i++) {
printf("%d ", ExampleArray[i]);
}
return 0;
}
We have the same array as before and use “length” to calculate the array size. After that, we access the third element (at index 2) and change its value from 6 to 10. Similarly, we modify the value of the last element at index 4, but we calculate it by adding the first and second elements together. This gives a new value of 6. We can see the output in the image below.

©History-Computer.com
Traversing the Array
Traversing simply means going through the array, element by element, usually in a sequence. This is a convenient way to access and modify the elements and is normally done using a for loop. For example:
#include <stdio.h>
int main() {
int ExampleArray[] = {2, 4, 6, 8, 10};
int length = sizeof(ExampleArray) / sizeof(ExampleArray[0]);
for (int i = 0; i < length; i++) {
printf("%d ", ExampleArray[i]);
}
return 0;
}
Here, we simply traversed the array after calculating its size and printed each element to the console. See the image for how this looks.

©History-Computer.com
Adding and Removing Elements
As we said, adding and removing elements aren’t inherent in C arrays. However, by simulating the array, we can accomplish a similar objective. This is done by using pointers and dynamic memory allocation. For an example, take a look at this code block:
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ExampleArray = NULL;
int size = 0;
int capacity = 0;
int initialElements[] = {2, 4, 6, 8, 10};
int initialElementsSize = sizeof(initialElements) / sizeof(initialElements[0]);
for (int i = 0; i < initialElementsSize; i++) {
if (size >= capacity) {
capacity = capacity == 0 ? 1 : capacity * 2;
ExampleArray = (int*)realloc(ExampleArray, capacity * sizeof(int));
}
ExampleArray[size] = initialElements[i];
size++;
}
printf("Array after adding elements: ");
for (int i = 0; i < size; i++) {
printf("%d ", ExampleArray[i]);
}
printf("\n");
if (size > 0) {
for (int i = 0; i < size - 1; i++) {
ExampleArray[i] = ExampleArray[i + 1];
}
size--;
printf("Array after removing first element: ");
for (int i = 0; i < size; i++) {
printf("%d ", ExampleArray[i]);
}
printf("\n");
}
free(ExampleArray);
return 0;
}
This looks complex, but we’ll explain each part. First, we include the standard library and input/output headers. We then declare a pointer used to allocate memory, “ExampleArray”, and initialize it to NULL.
Two integer variables are then declared, “size” and “capacity”, which are used to check the size of the array and the memory allocated.
An array called “initialElements” is created, with values 2, 4, 6, 8, and 10. The “sizeof” operator is used to calculate the array size, known as “initialElementsSize”.
A for loop is then initiated, which does most of the work. This increments over each array element, as shown by “i++”.
We use an if statement here, to check if the element number is greater or equal to the capacity. If so, the capacity is doubled and the array is resized using “realloc()”.
The elements are then assigned to positions in “ExampleArray”, and we print the array after adding the elements.
Finally, we modify the array. This is done by using an if statement and two for loops. First, we check if the size is greater than 0. If so, each element is shifted to the left by reducing its index number by 1, except for the last element. Effectively, this removes the first element. The result is then printed as well, which can be seen in the image below.

©History-Computer.com
What Are the Pros and Cons of Arrays in C?
Arrays are relatively simple to implement and allow for easy access and traversal. However, they’re usually fixed in C, without some potentially memory-intensive manipulation. As well as this, adding and removing elements can affect performance, since every time we do this, the elements need to be rearranged. When we need to do this regularly, it’s likely better to use a different data structure instead, such as a linked list.
Wrapping Up
Arrays are an extremely common data structure, and not only in C. While it may be difficult to adjust their size, they provide a convenient way to store, access, and traverse elements in a list. It’s worth remembering that element indices begin at 0 and not 1. The easiest way to access and traverse the elements in an array is by using a for loop. If you’re going to add and remove elements, be sure to allocate memory and free up memory after using malloc() and realloc(). It’s also essential to check your bounds since C does not do this automatically for you.
The image featured at the top of this post is ©Roman Samborskyi/Shutterstock.com.