c - Why this getchar() doesn't work properly? -
this question has answer here:
- getchar() skips every other char in c 2 answers
can tell me why line o = getchar();
works first time , once works, once it's not?
#include <stdio.h> int main(void) { char o; (int = 1; > 0; i++) { printf("%d\n", i); if (i % 10 == 0) { printf("do want continue? (y/n): "); o = getchar(); if (o == 'n') break; } } return 0; }
first of all, getchar()
returns int
, may not fit char
. change
char o;
to
int o = 0;
then, come point of skipping, well, not skip. when press key , press enter, newline
press of enter key serves feed getchar()
in every second iteration of loop.
Comments
Post a Comment