How to read an array in C language?
In C language, loops and input functions can be used to read an array. Here is a common approach:
#include <stdio.h>
int main() {
int n;
printf("请输入数组的长度:");
scanf("%d", &n);
int arr[n];
printf("请输入数组的元素:");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("您输入的数组为:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
The code above first reads the length of the array n, then creates an integer array arr of size n. Next, it traverses the array using a loop and uses the scanf() function to read in the value of each element one by one. Finally, it prints out the input array using another loop.
It is important to note that in actual programming, one should perform validity checks on inputs to ensure that the length of the array and the number of elements are reasonable.