C语言从控制台读取一行数据并回显
下面的程序,不停地从控制台读入一行数据并回显,直到遇到空行结束。
代码如下:
Please enter a line [blank line to terminate]> 123456
123456
Please enter a line [blank line to terminate]> abcxyz
abcxyz
Please enter a line [blank line to terminate] > C语言中文网
C语言中文网
Please enter a line [blank line to terminate]> game over
game over
Please enter a line [blank line to terminate]>
Press any key to continue
代码如下:
#include <stdio.h> int main(void) { char c; int count; for(;;){ count=0; printf("Please enter a line [blank line to terminate]> "); do{ c=getchar(); putchar(c); count++; }while (c!='\n'); if(count==1) break; } }可能的输出结果:
Please enter a line [blank line to terminate]> 123456
123456
Please enter a line [blank line to terminate]> abcxyz
abcxyz
Please enter a line [blank line to terminate] > C语言中文网
C语言中文网
Please enter a line [blank line to terminate]> game over
game over
Please enter a line [blank line to terminate]>
Press any key to continue