好的,我是第一次编程的学生,所以请善待;)这是在屏幕上打印“n”次字符串的正确代码...
#include <stdio.h>
#include <string.h>
#define MAX 80+1+1 /* 80+\n+\0 */
int main(void)
{
char message[MAX];
int i, n;
/* input phase */
printf("Input message: ");
i = 0;
do {
scanf("%c", &message[i]);
} while (message[i++] != '\n');
message[i] = '\0';
printf("Number of repetitions: ");
scanf("%d", &n);
/* output phase */
for (i=0; i<n; i++) {
printf("%s", message);
}
return 0;
}
为什么在 do-while 形式中他需要检查是否 message[i++] != '\n' 而不仅仅是 message[i] != '\n'?