Is this program doing what is supposed to do ?

  • Follow


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;
}
0
Reply devkays 1/30/2011 9:02:59 PM

On 01/31/11 10:02 AM, devkays wrote:
> 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.
>
Well what is it supposed to do?

> #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;
> }


-- 
Ian Collins
0
Reply Ian 1/30/2011 9:07:56 PM


1 Replies
104 Views

(page loaded in 0.029 seconds)


Reply: