difference between ((c=getchar())!=EOF) and ((c=getchar())!='~'),'~' can be any character -


as '~' encountered, after not printed , control comes out while loop

while((c = getchar()) != '~') {     putchar(c);     printf(" "); } input: asdf~jkl output: s d f   //control out of while loop 

as '^z' encountered, after not printed control doesn't come out of while loop

while((c = getchar()) != eof) {     putchar(c);     printf(" "); } input: asdf^zjkl output s d f ->   //control still inside while loop 

please explain why happening? eof encountered, while loop must exit, not happening. is necessary (ctrl+z) must first character on new line terminate while loop? has working of getchar() , eof (ctr+z)

it's way console input editor works in windows/dos command prompt. input done line line, why can go , forth editing characters until press enter, , @ point contents of line sent program , new line started.

whoever wrote editor in dos decided typing ^z @ beginning of line way tell editor you're done providing input.

the thing eof (end of file) virtual mark , not real character. actual value (-1) suggests being way outside range of character codes (which why it's important use int variable instead of char when calling getchar() , fgetc()).

in fact, example...

while((c = getchar()) != eof) {     putchar(c);     printf(" "); } 

...can work expect if run program using input redirection ("program.exe < input.txt") feed file ^z in middle. in case, there no command line editor in way.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -