How to input multiple lines of data in C language?
In C language, you can use loop structures and arrays to input multiple lines of data. Here is an example code:
#include <stdio.h>
#define MAX_LINES 100
#define MAX_LENGTH 100
int main() {
char lines[MAX_LINES][MAX_LENGTH];
int line_count = 0;
printf("请输入多行数据(输入空行结束):\n");
// 使用循环结构逐行读取数据
while (line_count < MAX_LINES) {
fgets(lines[line_count], MAX_LENGTH, stdin);
// 如果输入的是空行,则结束循环
if (lines[line_count][0] == '\n') {
break;
}
line_count++;
}
printf("输入的数据为:\n");
// 使用循环结构逐行输出数据
for (int i = 0; i < line_count; i++) {
printf("%s", lines[i]);
}
return 0;
}
In this example code, a two-dimensional character array called lines is defined to store multiple lines of input. The fgets function is used to read input data line by line and store it in the lines array. The loop terminates when an empty line is inputted. Finally, a loop structure is used to output the saved data line by line.