簡單,用fgets+迴圈就可以了。 一般不要用scanf,因為使用不好就不安全; 也儘量不要用gets, 具體參看C-FAQ 7.1。例子如下:#include <stdio.h>#include <string.h>#define BUFF_SIZE 128int main(int argc, char **argv){ int N = 3; char buffer[N][BUFF_SIZE]; char *p; int i; printf("Total input %d times.\n",N); for (i = 0; i < N; i++) { printf("%d: ", i); fgets(buffer[i], BUFF_SIZE, stdin); if((p = strchr(buffer[i], "\n")) != NULL) *p = "\0"; } printf("\nOutput:\n"); for (i=0; i<N;i++) printf("%s\n", buffer[i]); return 0;}
簡單,用fgets+迴圈就可以了。 一般不要用scanf,因為使用不好就不安全; 也儘量不要用gets, 具體參看C-FAQ 7.1。例子如下:#include <stdio.h>#include <string.h>#define BUFF_SIZE 128int main(int argc, char **argv){ int N = 3; char buffer[N][BUFF_SIZE]; char *p; int i; printf("Total input %d times.\n",N); for (i = 0; i < N; i++) { printf("%d: ", i); fgets(buffer[i], BUFF_SIZE, stdin); if((p = strchr(buffer[i], "\n")) != NULL) *p = "\0"; } printf("\nOutput:\n"); for (i=0; i<N;i++) printf("%s\n", buffer[i]); return 0;}