How to define the values of n arrays in the C language?
To define the values of n arrays, you can use a loop to assign them one by one. Here is an example code that can dynamically define the values of n arrays:
#include <stdio.h>
int main() {
int n; // 数组的个数
printf("请输入数组的个数:");
scanf("%d", &n);
int arrays[n]; // 定义包含n个元素的数组
// 循环赋值
for (int i = 0; i < n; i++) {
printf("请输入第%d个数组的值:", i + 1);
scanf("%d", &arrays[i]);
}
// 打印数组的值
printf("数组的值为:");
for (int i = 0; i < n; i++) {
printf("%d ", arrays[i]);
}
return 0;
}
In the code above, first the scanf function is used to get the number of elements in the array from the user. Then an array of n elements is defined using int arrays[n]. Next, in a loop, the scanf function is used to get the value of each element in the array from the user and assign it to the corresponding element. Finally, a loop is used to print the values of the array.
Please note that in the C standard, Variable Length Arrays (VLA) were introduced in the C99 standard and not all C compilers support them. If your compiler does not support VLA, you can use dynamic memory allocation to define the values of an array of size n.