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

Lesson 11: Arrays

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

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

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...
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 10: for & while Loops
Next lesson →
Lesson 12: 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.