C language learned by suffering
C language learned by suffering
Multiple variables are handled together.
Array Concepts
Until now, when declaring variables, we have always declared only one.
In some cases, however, a large number of variables may be needed.
For example, if you are creating a program to keep track of test scores for 100
Inevitably, 100 variables are needed.
There is no end to the number of variables that can be declared for each and every one of them.
In such cases, use arrays to declare multiple variables at once.
An array is a mechanism that allows multiple variables of the same type to be handled together.
Each variable declared as an array is distinguished by a number.
By switching the numbers around as appropriate, you can have 100's or 1000's of them.
A large number of variables can be handled in the same way.
In some cases, however, a large number of variables may be needed.
For example, if you are creating a program to keep track of test scores for 100
Inevitably, 100 variables are needed.
There is no end to the number of variables that can be declared for each and every one of them.
In such cases, use arrays to declare multiple variables at once.
An array is a mechanism that allows multiple variables of the same type to be handled together.
Keywords.
[Array
By declaring multiple variables of the same type and managing them by number
A mechanism that allows multiple data to be handled at once.
Each variable declared as an array is distinguished by a number.
By switching the numbers around as appropriate, you can have 100's or 1000's of them.
A large number of variables can be handled in the same way.
Array Declaration
Arrays are declared as follows
A type name means the same thing as the type of a variable up to now.
Variables of the type specified here are created for the number of elements.
An array name is the name of the entire array.
When individual variables are used, they are distinguished by numbering these names.
The number of elements is the number of variables to be created.
The number of variables of a given type will be created according to the number specified here.
Only integer values can be specified here as numbers.
Variables of integer type cannot be specified at the time of declaration.
The following is an example of declaring an array array with 100 variables of type int.
array (programming, programing)
Type name Array name[number of elements];
A type name means the same thing as the type of a variable up to now.
Variables of the type specified here are created for the number of elements.
An array name is the name of the entire array.
When individual variables are used, they are distinguished by numbering these names.
The number of elements is the number of variables to be created.
The number of variables of a given type will be created according to the number specified here.
Only integer values can be specified here as numbers.
Variables of integer type cannot be specified at the time of declaration.
These are the methods for declaring arrays.
The following is an example of declaring an array array with 100 variables of type int.
Declare array array
int array[100];
Array handling
Arrays are handled exactly the same as variables, except that array names are numbered.
To number array names, do the following
It is important to note that the numbering begins with the number 0.
For example, for the array with 100 elements declared in the previous section, the
The number that can be specified is 100, from 0 to 99, not 1 to 100.
Starting with the number 0 may seem confusing, but
Mathematically, it is more convenient in most cases to start from 0.
Adding 10 to the first number makes 10, so we can clearly see that we are 10 ahead.
As long as you are careful to start with the number 0, the rest is no different from an ordinary variable.
The following program is an example of using the 10th element of an array array.
The result of executing this program will be as follows
The results show that they are exactly the same as when using ordinary variables.
To number array names, do the following
Numbering sequence names
Sequence name [number].
It is important to note that the numbering begins with the number 0.
For example, for the array with 100 elements declared in the previous section, the
The number that can be specified is 100, from 0 to 99, not 1 to 100.
Starting with the number 0 may seem confusing, but
Mathematically, it is more convenient in most cases to start from 0.
Adding 10 to the first number makes 10, so we can clearly see that we are 10 ahead.
As long as you are careful to start with the number 0, the rest is no different from an ordinary variable.
The following program is an example of using the 10th element of an array array.
source code
#include <stdio.h>
int main(void)
{
int array[100];
array[9] = 100; /* 9 is the 10th */ because it starts from the number 0
printf("1:%d\n", array[9]);
array[9]++;
printf("2:%d\n", array[9]);
return 0;
}
The result of executing this program will be as follows
Execution Result
1:100
2:101
2:101
The results show that they are exactly the same as when using ordinary variables.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.