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 at once. Each value sits in a numbered slot — called an index — starting from zero.

Arrays matter because they let you work with groups of related data without writing repetitive code. If you need to add up 100 test scores, an array lets you loop through them once instead of writing 100 separate lines. They are also how C stores strings (which are just arrays of characters) and how you build more complex data structures later.

The trade-off is that arrays have a fixed size set when you create them. You cannot add a new slot later — you decide upfront how many values the array will hold, and that size does not change while the program runs.

Key Takeaways

  • An array declaration names the type, the array name, and the size in square brackets: int scores[10]; creates space for ten integers.
  • Array indexes start at zero, so a ten-slot array uses indexes 0 through 9, and trying to access index 10 causes undefined behavior.
  • You assign values to array slots using the index: scores[0] = 95; puts 95 in the first slot.
  • A for loop is the standard way to fill an array or read through all its values without writing each index by hand.
  • Arrays decay into pointers when passed to functions, which is why functions that take arrays also need a separate parameter for the array size.

The basic syntax for declaring an array

To create an array, you write the data type, the array name, and the number of elements in square brackets. Here is the pattern:

int numbers[5];

This line creates an array called numbers that holds five integers. The size — the number in the brackets — must be a constant known at compile time (a number you type directly, or a constant you defined earlier with #define). You cannot use a variable as the array size in standard C, because the compiler needs to know how much memory to reserve before the program runs.

The data type can be any C type: int, float, char, double, or even a structure you defined yourself. All elements in the array must be the same type.

Putting values into array slots

Once you have declared an array, you put values into it by using the array name and an index in square brackets. Remember that indexes start at zero:

numbers[0] = 10; numbers[1] = 20; numbers[2] = 30;

This puts 10 in the first slot, 20 in the second, and 30 in the third. If your array has five slots (indexes 0 through 4) and you try to write to numbers[5], the program will not stop you — it will write to whatever memory happens to be next to the array, corrupting other data. This is one of the most common bugs in C programs.

You can also initialize an array when you declare it, listing the values in curly braces:

int numbers[5] = {10, 20, 30, 40, 50};

If you provide fewer values than the array size, C fills the remaining slots with zeros. If you leave the size blank, C counts the values you provided:

int numbers[] = {10, 20, 30};

This creates an array of exactly three integers.

Reading values from an array

To get a value out of an array, you use the same index notation:

int first = numbers[0]; printf("%d\n", numbers[2]);

The first line stores the value from the first slot into the variable first. The second line prints the value from the third slot. You can use an array element anywhere you would use a regular variable of that type.

The most common way to read through an entire array is with a for loop. This loop prints all five values:

for (int i = 0; i < 5; i++) {   printf("%d\n", numbers[i]); }

The loop variable i starts at 0 and counts up to 4 (the condition i < 5 stops it before reaching 5). Each time through, numbers[i] refers to the next slot in the array.

Using loops to fill and process arrays

Loops are how you avoid typing each index by hand. To fill an array with user input, you loop and ask for each value:

int scores[10]; for (int i = 0; i < 10; i++) {   printf("Enter score %d: ", i + 1);   scanf("%d", &scores[i]); }

This asks the user for ten scores one at a time and stores each one in the next array slot. Notice the & before scores[i] in the scanf call — scanf needs the address of where to store the value, just as it does with regular variables.

To add up all the values in an array, you loop through and accumulate:

int sum = 0; for (int i = 0; i < 10; i++) {   sum = sum + scores[i]; }

After this loop, sum holds the total of all ten scores. The same pattern works for finding the highest value, counting how many meet a condition, or any other operation on the whole array.

Passing arrays to functions

When you pass an array to a function, you write the array name without the size or index:

void print_array(int arr[], int size) {   for (int i = 0; i < size; i++) {     printf("%d\n", arr[i]);   } } print_array(numbers, 5);

The function receives two parameters: the array and its size. The size is necessary because the function has no way to know how many slots the array has — when an array is passed to a function, it decays into a pointer to its first element, and a pointer alone does not carry size information.

Inside the function, you use the array parameter exactly as you would a local array. You can read from it and write to it. Any changes you make affect the original array in the calling code, because the function is working with the same memory, not a copy.

Common mistakes and how to avoid them

The most dangerous mistake is accessing an index that does not exist. If you declare int arr[10] and then write to arr[10] or arr[15], the program will not crash when ready — it will write to whatever memory is nearby, and the crash may happen much later or not at all. Always double-check your loop conditions and make sure they stop before the array size.

Another common error is forgetting that indexes start at zero. A ten-element array uses indexes 0 through 9, not 1 through 10. If you write a loop that goes from 1 to 10, you will skip the first element and try to access one past the end.

A third mistake is trying to change the array size after declaring it. Arrays have a fixed size — if you need to add more elements, you must declare a larger array and copy the old values into it. This is why many programs use dynamic memory allocation (with malloc) for data that grows, but that is a separate topic.

Frequently Asked Questions

What is the difference between an array and a pointer?

An array reserves a fixed block of memory and lets you access each element by index. A pointer is a variable that holds a memory address. When you pass an array to a function, it decays into a pointer to the first element, which is why functions need a separate size parameter. You can use pointer arithmetic to move through an array, but arrays and pointers are not the same thing.

Can I create an array with a size that changes while the program runs?

No — array size must be fixed at compile time. If you need a container that grows, you must use dynamic memory allocation with malloc and realloc, which lets you request more memory as needed. This is more complex but necessary for programs that do not know the data size in advance.

What happens if I declare an array but do not initialize it?

The array is created, but its slots contain garbage values — whatever happened to be in that memory before. Local arrays (declared inside functions) are not automatically zeroed. If you need all slots to start at zero, either initialize them explicitly or declare the array outside any function, where C automatically zeros it.

How do I copy one array into another?

You cannot assign one array to another with a single statement like arr1 = arr2. You must loop through and copy each element: for (int i = 0; i < size; i++) arr1[i] = arr2[i]; Alternatively, you can use the memcpy function from the standard library, which copies a block of memory.

Can an array hold different types of data?

No — all elements in an array must be the same type. If you need to store mixed types together, you use a structure (a custom data type that groups different fields) and then create an array of structures.