I use a linux system with glibc.

In general terms this is how I believed buffering worked in regards to stdin.

Working in a terminal emulator my keyboard is stdin and the characters I type are placed directly into stdin's buffer and once I hit <enter> a newline was generated and that was the cue for whichever input function I called in my program to start reading from stdin's buffer until some condition specified in my program was met. So, essentially stdin's buffer was line buffered. Using functions like getchar or fgets seemed to back up my thinking.

However, today I used fread on stdin for the first time and found this was not the case, at least not in this situation.

The man page states that it reads nmemb items of data. So, excluding EOF or an error, fread won't begin reading from stdin's buffer until I have entered nmemb or more items and the newline character. This is exactly the behaviour I observed and it seems to contradict my previous understanding.

From the glibc manual:

Code:
12.20.1 Buffering Concepts

  There are three different kinds of buffering strategies:


 
  • Characters written to or read from an unbuffered stream are transmitted individually to or from the file as soon as possible.
  • Characters written to a line buffered stream are transmitted to the file in blocks when a newline character is encountered.
  • Characters written to or read from a fully buffered stream are transmitted to or from the file in blocks of arbitrary size.
Newly opened streams are normally fully buffered, with one exception: a stream connected to an interactive device such as a terminal is initially line buffered....
Using fread, stdin's buffer isn't unbuffered, or entirely line buffered, so it must be fully buffered.

So, arbitrary size, in my example, would be nmemb and only once that amount has been reached (with the exception of EOF or an error) will a new line flush stdin's buffer. This implies that stdin's buffer is aware of nmemb.

Am I on the right track? And if so how and when does stdin's buffer become aware of fread's nmemb?

Also I'm a little confused by the three different kinds of buffering strategies. Bullet points 1 and 3 say "Characters written to or read from..." but the 2nd bullet point only mentions writing to a line buffered stream. Does this mean you can't read from a line buffered stream? Using say fgets for an example, isn't stdin line buffered in that example since fgets starts reading once a newline is generated?

I'm still a beginner in C and programming so my apologizes in advance for incorrect terminology or if the answer to my questions are obvious.

Thank you for your time.