It is the operating system
that provides those streams. On *nixen they are labeled with file descriptors 0 and 1 (and 2 for standard error).
C and C++ don't care about the way things actually work in the OS. They each model
an interface to them. C uses the fgets(), fprintf(), etc kind of stuff. C++ uses the iostream stuff. Hidden behind the scenes (in the case of C on *nixen, it is pretty direct, else it is a little more involved and/or convoluted) they both use the OS facilities to access those streams (or equivalent).
Keep in mind that "stream" here is a term meaning a byte source
or a byte sink
— something you get
bytes from or something you send
- Do C and C++ use separate/independent streams?
They use different abstractions to access those streams. By default C++'s abstraction is linked to C's abstraction. You can disassociate them if you wish.
- If so, do C++ streams such as cin and cout work differently than stdin and stdout?
In the abstraction, yes. Once the data gets to the OS's end of things, though, no.
- Why does stdin read from the keyboard by default?
Because that is how the OS does it by default.
- To further elaborate on the above question, is stdin connected to a device driver to receive input from the keyboard?
*Unless it has been redirected from a different stream source.
- In Linux, C program and a C++ program; when I type from the keyboard, do the keystrokes go directly to that processes stdin? (I think in C++ the input could be buffered?)
No. It would be sweet if they did, but they are first pretty heavily processed. There are actually really good reasons for this, and it is much more complicated than you'd think, but it is what it is. If it helps to understand this, remember that the keyboard is not a simple byte source, but a fairly complicated device that needs special OS code to communicate with; not all keyboards work the same.
- Why and how does C++ use streams, does cin and cout reserve space in memory(buffer) for input from the keyboard and how does the keystrokes know how to go to cin rather than stdin?
"Streams" here is a C++ term for a class of object for accessing an abstraction over byte sources and byte sinks.
Again, the bytes don't "go to" anything. The abstraction (cin, stdin, etc) simply provides the programmer a way to access the byte streams and nominally process them for our programming needs.
Consequently, you can freely mix fgets() and cin>> in your code.*
*Assuming you don't do something to confuse their ideas of what the input state is.
The ideas behind redirectable streams were fundamental to the design of Unix, and a revolutionary idea. It was widely copied by other OSes as well, including CP/M, which was DOS's direct ancestor (of sorts), which is Windows' ancestor. What it meant was that you can get data from any source, and even swap sources, and your program can still use it. Likewise, your program can output data that can be directed to any sink you desire without the program's needing to be any the wiser.
It is worth your time to pick up a book on the design and implementation of the Unix system. The top two in your google search are the droids you are looking for:
It is also worth looking at how C++ streams work. Take a look through the basic_streambuf (https://en.cppreference.com/w/cpp/io/basic_streambuf
) for implementation details. It boils down to how the
virtual functions are implemented.
The special streams
) are special
, implementation-defined black boxes, because making them work is again a bit more involved than you would think at first blush.
Hope this helps.