So far in your C programming journey, you've learned that a standard variable can hold exactly one piece of data at a time. But what happens when you need to store the ages of 100 students, or the temperatures for 365 days of the year? Creating 365 individual variables (like day1, day2, day3...) would be incredibly inefficient and impossible to manage.
This is where Arrays come to the rescue!
An array is a special, highly efficient data structure that can store a fixed-size, sequential collection of elements belonging to the same data type. You can think of an array as a neatly organized row of connected boxes, where each box holds a value, and you can instantly access any box by knowing its position number.
To declare an array in C, you must specify the type of elements it will store, provide a name for the array, and define the number of elements it can hold enclosed within square brackets [].
Syntax:
dataType arrayName[arraySize];
For example, to declare an array capable of holding four integers:
int myNumbers;
At this point, the array myNumbers exists in memory, but it holds random garbage values because we haven't assigned anything to it yet.
You can assign values to an array at the exact moment you declare it. This is done by placing a comma-separated list of values inside curly braces {}.
#include <stdio.h>int main() { // Declare and initialize an array of integers int myNumbers[] = {25, 50, 75, 100};
// Notice we didn't put a number in the brackets [] this time! // The C compiler is smart enough to see 4 items and set the size to 4 automatically. return 0; }
You can access a specific element in an array by referring to its index number.
Crucial Rule: In C (and almost all programming languages), array indexes start at 0, not 1. This means the first element is at index [0], the second is at index [1], and so on.
#include <stdio.h>int main() { int myNumbers[] = {25, 50, 75, 100};
// Printing the first element (25) printf("The first item is: %d\n", myNumbers[0]);
// Printing the third element (75) printf("The third item is: %d\n", myNumbers[2]);
return 0; }
Modifying an element inside an array is just as easy as accessing it. You use the index number and reassign a new value to it using the = operator.
#include <stdio.h>int main() { int myNumbers[] = {25, 50, 75, 100};
// Let's change the first element from 25 to 33 myNumbers[0] = 33;
printf("The new first item is: %d\n", myNumbers[0]);
return 0; }
Because arrays use numerical indexes starting from 0, they pair perfectly with for loops. You can write incredibly compact code that processes hundreds of array items using a single loop!
#include <stdio.h>int main() { int myNumbers[] = {25, 50, 75, 100}; int i;
// We loop exactly 4 times (for index 0, 1, 2, and 3) for (i = 0; i < 4; i++) { printf("Element at index %d is: %d\n", i, myNumbers[i]); }
return 0; }
sizeof Trick for Array LengthsUnlike modern languages like Python or Java, arrays in C do not have a built-in .length property. To find out how many elements are securely stored in an array, C developers use a famous sizeof() trick.
The sizeof operator returns the memory size in bytes, not the number of items. An integer is typically 4 bytes. If you have an array of 5 integers, sizeof(array) will return 20. To get the length, you divide the total array size by the size of a single element!
#include <stdio.h>int main() { int myNumbers[] = {10, 20, 30, 40, 50};
// Total size of array (20 bytes) divided by size of one int (4 bytes) = 5 int length = sizeof(myNumbers) / sizeof(myNumbers[0]);
printf("The array has %d elements.\n", length);
// Now we can use length in our loop securely! for (int i = 0; i < length; i++) { printf("%d\n", myNumbers[i]); }
return 0; }
Out of Bounds Error: If you create an array of size 3 (indexes 0, 1, 2) and try to access myNumbers[3] or myNumbers[10], C will not stop you. It will compile, but it will reach into random computer memory and return garbage data—or worse, severely crash your program. This is the root cause of the infamous "Buffer Overflow" security vulnerability. Always ensure your loops respect array boundaries!
Which index number accesses the FIRST element of an array in C?
How do you correctly find the number of elements in an array named `arr`?