J
John
The program is supposed to read lines from the standard input, then each line is printed on the standard output preceded by its line number. The program should have no built-on limit on how long a line it can handle.
So I wrote the following program in C, but I'm not sure that it is doing what it is supposed to do. I verified the source code against the solution in the back of the book and I'm pasting here what the solution in the back of the book is.
#include<stdio.h>
#include<stdlib.h>
int main(){
int ch;
int at_beginning = 1;
int line = 0;
while( (ch==getchar())!= EOF){
if(at_beginning == 1){
at_beginning = 0;
line+=1;
printf("%d ", line);
}
putchar(ch);
if(ch == '\n')
at_beginning = 1;
}
return EXIT_SUCCESS;
}
So I wrote the following program in C, but I'm not sure that it is doing what it is supposed to do. I verified the source code against the solution in the back of the book and I'm pasting here what the solution in the back of the book is.
#include<stdio.h>
#include<stdlib.h>
int main(){
int ch;
int at_beginning = 1;
int line = 0;
while( (ch==getchar())!= EOF){
if(at_beginning == 1){
at_beginning = 0;
line+=1;
printf("%d ", line);
}
putchar(ch);
if(ch == '\n')
at_beginning = 1;
}
return EXIT_SUCCESS;
}