下面是<<C和指针>>上一道简单程序
题目如下:
编写一个程序,从标准输入读取几行输入。每行输入要打印到标准输出上, 前面要加上行号。在编写这个程序时要试图让程序能够处理的的长度没有限制。
题目分析:
通过从输入中逐个字符读取而不是逐行读取, 可以避免行长度限制。在这个解决方案中,如果定义了TRUE和FALSE符号, 程序的可读性会更好一些**/
/********
从标准输入复制到标准输出,并输出行标号
********/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int line = 0;
int ch;
int at_beginning = 1;
/**********
读取字符并逐个处理它们。
**********/
while((ch = getchar())!=EOF){
/********* 如果我们位于一行的起始位置,打印行号********/
if(at_beginning == 1){
at_beginning = 0;
line++;
printf("%d ",line);
}
/*********打印字符,并对行尾进行检查***************/
putchar(ch);
if(ch == '/n')
at_beginning = 1;
}
return 0;
//return EXIT_SUCCESS;
}