← Back to list

A Beginner’s Guide to Understanding Arrays in C

Stop declaring a hundred variables and learn to love contiguous memory.

Pranay Nair · 2026-06-29 13:27 · 1 claps · 2.7 min read
#c-programming #c-programming-language #computer-science #software-engineering #programming
Open on Medium ↗
Wiki topics: 💻 · Programming 🔬 · Science · General 💑 · Relationships

A Beginner’s Guide to Understanding Arrays in C

Stop declaring a hundred variables and learn to love contiguous memory.

If you are just starting your journey in Computer Science, C is often the place where you build your foundational logic. At first, declaring variables is simple: int a = 10; int b = 20;. But what happens when you need to store the exam scores of an entire class of 60 students?

Typing out int score1, score2, score3 … all the way to 60 is a nightmare. This is exactly where arrays come to the rescue.

Let’s break down what arrays are in C, how they work under the hood, and how to use them without running into those infamous segmentation faults.

What is an Array?

Think of an array like a row of post office boxes. Instead of renting 60 different boxes scattered all over town, you rent a single block of 60 boxes right next to each other. They all share the same address, but each box has a unique number.

In programming terms, an array is a collection of items of the same data type stored at contiguous memory locations. Instead of 60 different variables, you have one variable name that holds 60 values.

Declaring and Initializing Arrays in C.

To create an array in C, you need to tell the compiler three things: the data type, the name of the array, and the size (how many items it will hold).

// Syntax: data_type array_name[array_size];

int scores[5]; // Declares an array that can hold 5 integers

You can also give values to the array when you create it. This is called initialization:

int scores[5] = {85, 92, 78, 90, 88};

💡:If you initialize the array right away, you can leave the square brackets empty. The C compiler can count the elements for you: int scores[] = {85, 92, 78, 90, 88};

The Golden Rule: Zero-Based Indexing:

This is the number one trap for beginners. When humans count, we start at 1. When C counts arrays, it starts at 0. To access a specific value in our scores array, we use an index.

  • The 1st element is at index [0]
  • The 2nd element is at index [1]
  • The 5th element is at index [4]
printf("The first score is %d\n", scores[0]); // Prints 85
printf("The last score is %d\n", scores[4]);  // Prints 88

Navigating Arrays with Loops

Arrays and for loops are best friends. Because arrays use numerical indexes, a for loop is the perfect tool to iterate through them. When preparing for heavy end-semester exams, one of the best ways to truly understand how arrays interact with loops is to dry-run the code on paper. Grab a pen, draw out the memory blocks, and trace the value of i at each step. It works wonders for solidifying the logic. Here is how you print every score in the array:

#include <stdio.h>

int main() {
    int scores[5] = {85, 92, 78, 90, 88};

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

    return 0;
}

The Danger Zone: Out of Bounds

C is a powerful language, but it expects you to know what you are doing. It does not check if you are playing within the rules. If you create an array of size 5, the valid indexes are 0 through 4. If you try to access scores[10], C will not stop you from compiling. Instead, it will happily go to the memory address where the 11th element would be and grab whatever garbage data is sitting there.

⚠️Warning: Accessing memory outside your array limits can lead to unpredictable behavior, corrupted data, or the dreaded Segmentation Fault (core dumped), which crashes your program entirely. Always double-check your loop conditions!

Summary

  • Arrays store multiple items of the same data type.
  • They are stored in contiguous (side-by-side) memory locations.
  • The first element is always at index 0.
  • The last element is at index [size — 1].
  • Always ensure you don’t go out of bounds.

Arrays are just the beginning. Once you master them, you unlock the door to strings (which are just arrays of characters in C), matrices (2D arrays), and eventually complex Data Structures and Algorithms. Keep coding, keep dry-running your loops, and embrace the fundamentals!


메타데이터
post_id
50709bacd07b
slug
a-beginners-guide-to-understanding-arrays-in-c-50709bacd07b
url
https://medium.com/@nairpranay35/a-beginners-guide-to-understanding-arrays-in-c-50709bacd07b
canonical_url
https://medium.com/@nairpranay35/a-beginners-guide-to-understanding-arrays-in-c-50709bacd07b
author_url
https://medium.com/@nairpranay35
status
ok
fetched_at
2026-07-29 08:46:23