Reputation: 174
I'm having trouble understanding Input/Output, standard streams, stdin, EOF and other topics. My question is that when I run getchar()
multiple times consecutively, one is cancelled:
getchar();
getchar();
I expected it to prompt for input two times, but it only did once. In higher level languages like Python, it is very easy to prompt multiple times:
input()
input()
And when I run getchar()
more than two times:
getchar();
getchar();
getchar();
getchar();
getchar();
In this case, 5 times, I get prompted 3 times (at least for me, I tried it again and I still get prompted 3 times). I don't know what is going on, I even tried to put sleep(1)
between the statements but I still get the same outcome.
I expect getchar()
to be blocking, since it is in Canonical Mode. My hypothesis is that getchar()
is somewhat asynchronous, and that the next statement is executed without waiting for the user to press Enter.
Info:
I'm just curious how getchar()
works. (I also checked some Stack Overflow questions but none seemed to be related to my problem)
EDIT: [SOLVED]:
@Samarthya Singh's comment
I've even created a function that stops that from happening:
int getch() {
int c;
c = getchar();
if (c == '\n')
return getchar();
return c;
}
Upvotes: 0
Views: 70
Reputation: 174
SOLVED:
@Samarthya Singh's comment
Maybe it's taking the '\n' as the second character.
I've even created a function that stops that from happening:
int getch() {
int c;
c = getchar();
if (c == '\n')
return getchar();
return c;
}
Upvotes: 0