0
#include<stdio.h>
int main(){
   int c = getchar();
   while(c != EOF){
      putchar(c);
      c = getchar();
   }
}

In above code why does not the program terminates by itself after c becomes EOF? Reference of the code > Book: K&R's The C Programming Language 2nd Edition, Page: 18

  • It terminates for me when I type as an input in an Ubuntu Linux terminal. Just hitting return won't cause getchar() to return EOF (-1). – Scooter Nov 15 '17 at 05:16
  • Possible duplicate of [getchar() != EOF](https://stackoverflow.com/questions/27183865/getchar-eof) – Raghav Dinesh Feb 14 '19 at 06:09

1 Answers1

0

getchar() will return EOF only if the end of file is reached. The ‘file’ here is the standard input itself. This can be written as:

#include <stdio.h>
   int main()
  {
   int c;
   while ((c = getchar()) != EOF)
    {
     /*getchar() returns the the next available value which is in the input
     buffer*/    
     putchar(c);
    }
  }
Nabid
  • 197
  • 5
  • 24