MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Handling multiple variables collectively.

The concept of arrays
"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.

キーワード
【array】

同じ型のvariableを複数宣言し、番号によって管理することで、
複数のデータを一括して取り扱えるようにする仕組み。


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.

Source code
#include <stdio.h>

int main(void)
{
    int array[100];

    array[9] = 100; /* 0番からなので9が10番目 */
    printf("1:%d\n", array[9]);
    array[9]++;
    printf("2:%d\n", array[9]);

    return 0;
}

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.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19