How to input an array in the C language?
In C language, a loop statement and the scanf function can be used to input an array.
Here is an example code for inputting an array of integers:
#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;
}
After running the program, first enter the size of the array, then input each element of the array one by one. Finally, print out the inputted array.
It is important to note that in C language, the size of an array must be a constant expression, meaning a value determined at compile time. Therefore, using a variable like ‘n’ to dynamically define the size of an array is only applicable in C99 standard and above. For earlier C standards, one would need to use fixed-size arrays or dynamically allocate memory to input arrays.