I'm reading Programming in C - A Tutorial by Brian Kernighan and on page 5 he suggests that
main( ) {
char c;
while( (c=getchar( )) != ′\0′ )
putchar(c);
}
can be simplified to
main( ) {
while( putchar(getchar( )) != ′\0′ ) ;
}
with the only difference being that the last '\0'
is printed in the 2nd one.
However, when I compile this, replacing '\0'
with EOF
, and pass a string: $ printf "abc" | ./a.out
, the program goes into an infinite loop printing ASCII 0xff
characters.
If I change it to while( putchar(getchar( )) != 'd' ) ;
and run $ printf "abcde" | ./a.out
, it successfully prints up to and including d
and then exits.
Why does it go into an endless loop instead of printing abc(EOF)
and exiting?