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

Lesson 11: 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 from 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 #1 beginner mistake in C.
● In the declaration, [5] means "how many" β†’ 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. Drill it in that the declaration count 5 and the last valid index 4 differ by one.

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

Array memory layout

Array elements are stored contiguously in memory. Click a cell to inspect.
Click an array 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 with 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

2D arrays are stored in a single contiguous line in memory (row-major order).
matrix[1][2] is the third element of row 1 = 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 computations are typical uses of 2D arrays.
When passing to a function, you must specify the column count: void func(int m[][4], int rows).

Try it yourself — array

my_array.c
Output
Click "Run" to execute...
πŸ’‘ Try these ideas too
Advertisement

Related Lessons

Loops
Lesson 10: for & while Loops
How to use for and while loops in C.
Arrays & Strings
Lesson 12: 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 17: Random Numbers
Next lesson →
Lesson 19: Strings

Review Quiz

Check your understanding of this lesson!

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

4
5
6

a[5] has 5 elements from a[0] to a[4].

Q2. What index does a C array start from?

0
1
Any

C array indices always start at 0. For 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 you partially initialize an array, the remaining elements are zero-initialized. a[2] is 0.

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

Recommended Books to Deepen Your Understanding

Combine this interactive site with books for more practice.

πŸ“˜
Painfully Learning C
by MMGames
A classic C book for beginners. Builds solid fundamentals with clear explanations.
View on Amazon
πŸ“—
New Clear C Language (Beginner)
by Bohyoh Shibata
Plenty of diagrams and exercises. Widely used as a university textbook.
View on Amazon
πŸ“™
The C Programming Language, 2nd Ed.
by Brian Kernighan & Dennis Ritchie
Known as K&R. The original C book. Ideal after completing the basics.
View on Amazon

* Links above are affiliate links. Purchases help support this site.