Why does this getchar() loop stop after one character has been entered?
Posted
by Pieter
on Stack Overflow
See other posts from Stack Overflow
or by Pieter
Published on 2010-03-17T13:55:55Z
Indexed on
2010/03/17
14:11 UTC
Read the original article
Hit count: 171
#include <stdio.h>
int main() {
char read = ' ';
while ((read = getchar()) != '\n') {
putchar(read);
}
return 0;
}
My input is f
(followed by an enter, of course). I expect getchar()
to ask for input again, but instead the program is terminated. How come? How can I fix this?
© Stack Overflow or respective owner