c - A loop to read characters from console till EOF character is encountered using getchar() is not exiting when EOF character is encountered -
this question has answer here:
- why 2 eof needed input? [duplicate] 2 answers
- why require multiple eof (ctrl+z) characters? 5 answers
here program read character console , print them in reverse order.
#include<stdio.h> main() { int ch, count = 0; char a[100]; printf("enter charachters\n"); ch = getchar(); while(ch != eof && count<100) { a[count] = ch; count = count+1; ch = getchar(); } printf("\ncount = %d\n",count); while (count>0) { count = count -1 ; putchar(a[count]); } }
my questinon is: when give eof character (ctrl+d) after typing in few characters on console, not exit out of loop. not add count variable not exit loop. if eof character first character after newline character, read , loop exited. eg if sample input is:
abcdef
abc
ctrl+d
then code works fine if input is:
abcdef ctrl+d
the loop not exited. tell me way accomplish this.
thanks
ctrl-d eot (end of transmission). ctrl-z eof (end of file). isn't specific unix ascii. unix libraries choose interpret eot way signal end of input on character i/o.
Comments
Post a Comment