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

ouput

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

https://unix.stackexchange.com/a/177662/6555


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -