c - getchar() continues to accept input after including Ctrl+Z in same line -
simple c program accept , print character.
int c; while((c=getchar())!=eof) { putchar(c); }
i not getting why accept input when press ctrl+z @ end of line ex hello(press ctrl+z) hello (some symbol) work after leaving line pressing ctrl+z. , using window 7
when call getchar()
in turn ends making read()
system call. read()
block until has characters available. terminal driver makes characters available when press return key or key signifying end of input. @ least looks like. in reality, it's more involved.
on assumption ctrl-z mean whatever keystroke combination means "end of input", reason way read()
system call works. ctrl-d (it's ctrl-d on unixes) character doesn't mean "end of input", means "send current pending input application".
when you've typed in before pressing ctrl-d, input gets sent application blocked on read()
system call. read()
fills buffer input , returns number of bytes put in buffer.
when press ctrl-d without input pending (i.e. last thing didwas hit return
or ctrl-d, same thing happens there no characters, read()
returns 0. read()
returning 0 convention end of input. when getchar()
sees this, returns eof
calling program.
this answer in stack exchange puts bit more clearly
Comments
Post a Comment