编写一个程序,从标准输入读取几行输入。每行输入都要打印到标准输出上,前面加上行号。在编写这个程序的时候要使这个程序能够处理的输入行的长度没有限制。

    题目要求程序能够处理的输入行没有限制,可以利用getchar()从输入端上获取每行的字符,但是对于getchar(),只能每次获取一个字符,当获取到'\0'时,输出行号,结果与题目不符。可以借用一个flag,当flag为1时,输出行号。

#include <stdio.h>
int main()
{
    int ch=0;
    int i=1;
    int flag=1;
    while((ch=getchar())!=EOF)
    {
        if(flag==1)
       {
            printf("%d ",i);
            flag=0;
       }
       if(ch=='\n')
       {
            i++;
            flag=1;
       }
       putchar(ch);
    }
return 0;
}