I'm working on implementing a lexer and I noticed that the first call to getchar() always returns a character of junk before it prompts me for any input. I wrote a small test program to reproduce this:
Code:
#include <stdlib.h>
#include <stdio.h>
int main() {
int c;
for ( ; ; c = getchar()) {
printf("%c\n", c);
}
return 0;
}
Below is a run of my program:
Code:
$ ./getchar_test
P
I didn't write that!
I
d
i
d
n
'
t
w
r
i
t
e
t
h
a
t
!
Is there junk in my stdin? Can I clear it out? Am I just overlooking something obvious? That extra character seems to always be the same per program. For this test, it seems to always be 'P', In my lexer it is character value 0.
Thanks