πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English

Lesson 20: Arrays

Learn how to declare, initialize, and access C arrays, with clear memory diagrams.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • int a[5]; declares an array of 5
  • Index from a[0] to a[4]
  • Out-of-bounds access is a bug
⭐ Read if you have time
  • Initializer: int a[] = {1,2,3};
  • 2D arrays: int m[3][4]
  • sizeof(a)/sizeof(a[0]) for element count

Array — group values of the same type

Indices (subscripts) start at 0.
int a[5] = {10, 20, 30, 40, 50};
printf("%d\n", a[0]); // -> 10
printf("%d\n", a[4]); // -> 50
⚠️ The 5 in int a[5] is the count. a[5] itself does not exist!
This is the top beginner mistake in C.
● In the declaration, [5] means "how many" β†’ it reserves space for 5 elements.
● In an access, [ ] means "which one" β†’ only 0 through 4 are valid.

int a[5]; // reserves 5 slots: a[0], a[1], a[2], a[3], a[4]
a[0] = 10; // βœ… first element
a[4] = 50; // βœ… last element
a[5] = 99; // ❌ out of range β€” writes into memory that isn't yours (undefined behavior)

You get a[0] through a[4] β€” exactly 5 slots. Remember: the declaration size 5 and the last valid index 4 are always off by one.

Why? An index is "how far from the start." The first element is "0 away," and with 5 slots the last is "4 away."
πŸ’‘ How bad is out-of-range access? C won't flag a[5] or a[100] as an error (often not even a warning). At runtime it may clobber another variable, crash with a Segmentation fault, or β€” worst of all β€” appear to work while quietly corrupting data.
That's why loops use i < 5 (strict less-than) for "0 through 4." Writing i <= 5 would touch a[5] β€” the classic off-by-one bug.

Array memory layout

Array elements sit contiguously in memory. Click a cell to inspect it.
Click a cell to see its details.

Step execution — array with for loop

array_demo.c

Variable state

NameTypeValue

Array memory

Standard output

 

2D arrays

For tabular data with rows and columns, use a 2D array.
Declare it as type name[rows][cols];.

Declare and initialize

// 3 rows x 4 columns
int matrix[3][4] = {
    {1,  2,  3,  4},   // row 0
    {5,  6,  7,  8},   // row 1
    {9, 10, 11, 12}    // row 2
};

Memory layout

In memory, a 2D array sits in one contiguous line (row-major order).
matrix[1][2] is the third element of row 1, which is 7.
1
2
3
4
5
6
7
8
9
10
11
12
Blue = row 0, green = row 1 (orange = matrix[1][2]), red = row 2

Traverse with nested loops

for (int i = 0; i < 3; i++) {        // row
    for (int j = 0; j < 4; j++) {    // column
        printf("%3d", matrix[i][j]);
    }
    printf("\n");
}
Multiplication tables and matrix math are classic use cases for 2D arrays.
When you pass one to a function, you must specify the column count: void func(int m[][4], int rows).

Try it yourself — array

my_array.c
Output
Press "Run" to execute...
⚠️ Is "Average = 80" really right? β€” the integer-division trap
The sum is 80+65+92+78+88 = 403, so the true average is 403 Γ· 5 = 80.6.
But the program prints 80. That's because in C, int divided by int truncates toward zero (int / int β†’ int).
To get 80.6 properly:
Cast one operand to double: double avg = (double)sum / 5;
Then print with printf("Average = %.1f\n", avg); (%.1f = one decimal place).
πŸ’‘ "Integer division truncates" is the top gotcha for C beginners. See Lesson 8 (Arithmetic operators) for more.
πŸ’‘ Try these ideas too

Related Lessons

Loops
Lesson 11: for & while Loops
How to use for and while loops in C.
Arrays & Strings
Lesson 22: Strings
How to work with strings in C: char arrays, strcpy, strlen, strcmp.
Reference
C Cheat Sheet
Quick reference for printf, operators, types, and more.
← Previous lesson
Lesson 19: Debugging
Next lesson →
Lesson 21: Quiz (Arrays)

Review Quiz

Check your understanding of this lesson!

Q1. How many elements does int a[5]; allocate?

4
5
6

a[5] gives you 5 elements, from a[0] through a[4].

Q2. What index does a C array start from?

0
1
Any

C array indices always start at 0. In a[5], a[0] is the first element.

Q3. For int a[3] = {1, 2};, what is the value of a[2]?

Undefined
0
2

When an array is partially initialized, the leftover elements are zero-initialized. So a[2] is 0.

Share this article
Share on X (Twitter) Share on Facebook Send on LINE Hatena