The following output seems to suggest that when read () is called after a getchar() it clears all the stdin. Let's say that I typed "Hello\n" :
char buff; int c; c=getchar(); printf("%c\n",c); //output H int res; res=read(0,&buff,1); //no output, it prompts !! printf("%c\n",buff); However if I invert the functions the behaviour is different. Let's say I type "Hello\n" again :
char buff; int res; res=read(0,&buff,1); printf("%c\n",buff); output H int c; c=getchar(); printf("%c\n",c); output e Could someone explain what's going on here ? I'm using Mac OS
readis unbuffered. So it is a bad idea to mix it with buffered IO. I'd guessgetcharhave already got all of the input into the buffer so nothing is left toread.setvbuf( stdin, NULL, _IONBF, 0 );to disable buffering.readto block. If the input stream is short and closed, I would expectreadto immediately return without writing to buff and the subsequentprintfto invoke undefined behavior. But there's no "prompt" anywhere.readdoes nothing at all with the stdin buffer. It doesn't read from that buffer, nor does it flush or fill or update it in any way.