Introduction to C language one-dimensional array

Recognize arrays

When defining an array:
int array[10];

	数组类型:int
	  数组名:array
    元素个数:10

When using an array:
array[10]

数组名:array
  下标:10

What is an array?

An array is a kind of container (a container for things), which can be imagined as a cabinet! The
first layer is the first element array[0], the
second layer is the second element array[1], and the
third layer is the third element array[ 2]
…the
tenth level is the tenth element array[9]
Insert picture description here

Array characteristics

1. All the elements have the same data type
2. The address between the two elements is continuous
3. Once created, the size cannot be changed

Notes on arrays

1. The subscript starts counting from 0, that is, the effective subscript range is [0, the size of the array-1]
2. The input and output of the array need to traverse the array (usually use the for loop to traverse the input and traverse the output)
3. The array is best Initialize first (syntactically, it is legal to use directly without initializing, and no error will be reported)

Use of arrays

Because an array is a container for storing things, just like a cabinet.
If you want to fill the cabinets, you need to open the cabinets on each floor in turn, and put things in-----this is the traversal input; to take out all the things in the cabinets, you need to open each cabinet in turn and take them out -----This is the traversal output.
The initialization of the array can be regarded as the same thing in the cabinet, for example: air

Array initialization assignment and output

#include<stdio.h>
int main(){
    
    
	int array[10]={
    
    '\0'}; //定义一个有10个元素大小的整型数组 并初始化赋值
	int i;
	int x;
	
	for(i=0;i<10;i++){
    
       //遍历输出
		printf("array[%d]=%d\n",i,array[i]);
	}
	return 0;
}

operation result:
Output result

Array input and output

#include<stdio.h>
int main(){
    
    
	int array[10]={
    
    '\0'}; //定义一个有10个元素大小的整型数组 并初始化赋值
	int i;
	int x;
	
	for(i=0;i<10;i++){
    
      //遍历输入(动态赋值)
		scanf("%d",&x);
		array[i]=x;
	}
	putchar('\n');		//换行
	for(i=0;i<10;i++){
    
      //遍历输出 
		printf("array[%d]=%d\n",i,array[i]);
	}
	return 0;
}

operation result:
Iterate over the results of input and output

Guess you like

Origin blog.csdn.net/weixin_49472648/article/details/107557546