C & C++ fundamentals / 10

Arrays

Store multiple values in an array, use zero-based indices, and read and print array elements with loops.

On this page

An array lets you group multiple values under one name. Declaring a separate variable for every value quickly makes a program cumbersome, so arrays are a useful way to organize repeated data.

In a declaration such as int arr[10], arr is the array name and 10 is the number of elements. The size goes inside square brackets.

Read and print three elements

#include <stdio.h>

int main()
{
    int arr[3];

    scanf("%d%d%d",&arr[0],&arr[1],&arr[2]);
    printf("%d %d %d",arr[0],arr[1],arr[2]);

    return 0;

}

This array stores three integers. Array indices begin at zero, so the three elements are arr[0], arr[1], and arr[2]. The program reads a value into each one and then prints them using the same indices.

Use a loop

Writing out every element becomes impractical when an array has hundreds or thousands of values. We can use the loops from the previous lessons instead:

#include <stdio.h>

int main()
{
    int arr[10];

    for(int i=0;i<10;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(int j=0;j<10;j++)
    {
        printf("%d",arr[j]);
    }
    return 0;
}

This example uses ten elements so it is easy to test by hand. The first for loop reads the values in order, and the second prints them.

Translation note: the indices in this example are 0 through 9, not 0 through 10 as the original closing sentence implied. The declared size is ten, and both loops use < 10.

Moved here from Tistory

I migrated this post from my Korean Tistory blog, I am Jason Lee, to this website and translated it into English. My writing and projects now live together in one place.

The original publication date, code examples, and screenshots are preserved. Editorial notes clarify known issues in the original material.

Original post on Tistory English edition · Aug 30, 2026
← Back to all articles