How to input data using a for loop in the C language?

In C language, you can use a for loop to repeatedly input data. Here is an example code:

#include <stdio.h>

int main() {
    int n;
    printf("请输入要输入的数据个数:");
    scanf("%d", &n);

    int data[n];
    printf("请输入数据:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &data[i]);
    }

    printf("输入的数据为:");
    for (int i = 0; i < n; i++) {
        printf("%d ", data[i]);
    }
    printf("\n");

    return 0;
}

In the above code, first the number of data n is input using the scanf function, and then an array called data is used to store the input data. Next, a for loop is used to repeat n times, each time inputting a piece of data using the scanf function and storing it in the corresponding position of the data array. Finally, another for loop is used to traverse the data array and output the input data to the screen.

bannerAds