EOF problem with getchar()

Bharat Mallapur bharatmallapur@eth.net
Mon Apr 10 08:46:00 GMT 2000


Bug report for gcc version 2.95.2 19991024 (release)
contact me by e-mail at : guruduttmallapur@usa.net

I am running MS-DOS (Windows 98 04-10-1998 release)
Celeron 366 MHz.
RAM 64 MB

The gcc that i am using was recently downloaded as part of the djgpp set of
zip files.

My problem is the following:
============================
/*
 Problem : Non leading EOF is ignored by getchar().
 Triggering :
             Try TYPING IN the 3 following lines when running the program:
             "a ^Z the" enter/return
             " ^Z the"  enter/return
             "^Z the"   enter/return
             (here ^Z gives EOF and is same as Ctrl+Z)

 My apologies if this is a wild goose chase.
 And thanks for making gcc free.Its very essential/useful for students
 like me.
*/

#include <stdio.h>

int main(void)
{
 int ch=0;

 while (1) {
    if ( (ch=getchar()) == EOF ) {
        printf("EOF in input value=%d and ch=%c", ch,ch);
        break;
    }
    else {
        printf("%c",ch);
    }
 }
 return 0;
}
/*
 Read on :

 The problem seems to be with getchar() dealing with EOF in 'interactive
mode'
 It works well when piping input from a text file and that too because the
EOF
 has already been correctly dealt with by the editor used to create the
file.

 On typing the 3 following lines when running the program:
 "a ^Z the" enter/return
 " ^Z the"  enter/return
 "^Z the"   enter/return

 1) a leading EOF causes getchar() to behave properly
 2) a non leading EOF causes getchar() to
    a) stop inputting characters
    b) ignore the EOF as seen by the non-triggering of the if-else

 You may say its not a big deal but then if I were to write the above
 program it would loop infinitely unless I entered a leading EOF.
 Also this very same program drew my attention to the problem.

 The fact is, it is based on an exercise program in
 Kernighan and Ritchie second edition C prog. lang.

 There is no problem in this program as getchar is not called again
 and again as in a loop to input a string.

 #include <stdio.h>

 int main(void)
 {
  int ch=0;

  if ( (ch=getchar()) == EOF ) {
     printf("EOF in input value=%d and ch=%c", ch,ch);
  }
  else {
      printf("%c",ch);
  }
  return 0;
 }

*/





More information about the Gcc-bugs mailing list