"Previously, we always declared variables one at a time."
However, there are cases where a large number of variables are required.
For example, if you were to create a program to manage the scores of 100 test takers,
It's become unavoidable that we'll need 100 variables.
If we declared one variable at a time, we'd be at it forever.
In such cases, you can use a array to declare multiple variables at once.
An array is a mechanism that allows you to handle multiple variables of the same type as a group.
Variables declared as arrays are distinguished individually by their index.
By switching between numbers as needed, whether it's 100 or 1000,
You will be able to handle a large number of variables consistently.
Array Declaration
Arrays are declared as follows.
array
型名 array名[要素数];
Type name means the same as the types of variables we have had so far.
Variables of the specified type will be created for each element.
An array name refers to the name of the entire array.
When using individual variables, we differentiate them by numbering this name.
"The number of elements refers to the number of variables created."
"A variable of the specified type is created the specified number of times."
Only integer values can be specified here as numbers.
You cannot specify an integer variable at the time of declaration.
That's how you declare arrays.
Here's an example of declaring an array named 'array' that holds 100 integer variables.
Declare an array.
int array[100];
Array Handling
Working with arrays is essentially the same as working with variables, except for the need to specify a number after the array name.
To number array names, do the following.
Numbering array names
array名[番号]
Please note that the numbers start from 0.
For example, in the case of the array with 100 elements declared in the previous section,
There are 100 numbers available, from 0 to 99.It's not between 1 and 100.
It might be confusing that it starts from zero,
Mathematically, it's often more convenient to start from zero.
"If you add 10 to the first number, it becomes 10, clearly indicating that it is ten places ahead."
"If you just remember that it starts at index 0, it's no different from any other variable."
The following program demonstrates the use of the tenth element of array array.
The results of this program's execution are as follows:
Execution results
1:100 2:101
The results show that it's exactly the same as when using a regular variable.
About This Site
Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.