Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Introduction to Arrays | Data Types and Variables
C Basics (copy)

bookIntroduction to Arrays

メニューを表示するにはスワイプしてください

Sometimes you need to create hundreds or even thousands of variables. Creating them one by one isn't practical. That's where arrays come in. An array is a collection of variables of the same type. If a single variable is like one storage box, an array is a warehouse filled with boxes, each holding its own value. Declaring an array looks something like this:

int array[3];

Here's how you declare an array with space for three elements. To assign values to it, use curly braces to list them inside.

int array[3] = {1, 5, 10};
int array[] = {56, 3, 10};
Note
Note

If you specify the items directly, you don't need to declare the size, the compiler automatically counts and assigns the number of elements.

Indexes

Each box in an array has its own unique identifier, called an index, which lets you easily access specific elements. An index is the number assigned to each item in the array, similar to your position in line at a coffee shop. Indexes in C start at zero, meaning the first element has an index of 0.

Main.c

Main.c

copy
12345678910
#include <stdio.h> int main() { // Array declaration int array[3] = {56, 3, 10}; // Display the first element of the array printf("%d", array[0]); return 0; }
Note
Note

The arrays discussed so far are static, meaning their size stays fixed during the program’s execution. There are also dynamic arrays, which can change size while the program is running.

You can change the value of any element in an array by referring to its specific index.

main.c

main.c

copy
123456789101112131415161718
#include <stdio.h> int main() { int array[3] = { 56, 3, 10 }; printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); array[1] = 555; // change 10 to 555 printf("%d ", array[0]); printf("%d ", array[1]); printf("%d\n", array[2]); return 0; }
question mark

Given the following array declaration in C, what value will be printed by the code below?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  4

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 2.  4
some-alt