1

Say we have:

std::cout << "Something";

How exactly is this working? I just want to make sure I understand this well and, from what I've been reading, is it okay to say that basically the insertion operator inserts the string literal "Something" into the standard output stream?

But what happens after that? Where does the standard output stream lead? Can anyone explain this?

That's basically the only part I don't get: I have the string literal "Something" in the standard output stream, but where does the stream lead?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

2 Answers2

0

The technical details vary between the different Operating Systems, but the basics are the same:

Every program has usually 3 standard streams: out (cout), in (cin), and err (cerr) (same as out, but used for errors). Those streams are nothing on their own; they exist to be used by a third party. That third party may be, for example, the terminal. When you execute a program form the terminal, it attaches to the program streams and show their output/request their input in the terminal.

If you wanted to do the same, you could execute a command yourself from your program, and take the out/in/err streams to read or write from/to them. You have an example of that here: How do I execute a command and get the output of the command within C++ using POSIX?

Edit: When talking about C++, remember that cout << "anything" is just syntactic sugar for the function cout.operator<<("anything"). And that function "simply" writes to the stream

Iván
  • 971
  • 6
  • 20
  • Okay but still not exactly what I wanted um just one tiny thing so like, cout << "something"; does that put something right into the stream and then it goes straight into the program or like is there something going into the buffer here? –  Sep 06 '21 at 20:48
  • About if it is buffered, yes. You'll get a better answer here: https://stackoverflow.com/questions/26975876/is-stdcout-buffered – Iván Sep 06 '21 at 21:09
  • There is a lot of detail missing in this answer, for instance the existence of `std::streambuf` how it is assigned to `std::cin`/`std::cout`/`std::cerr`, and how it is linked to `stdin`/`stdout`/`stderr`. – Remy Lebeau Sep 06 '21 at 21:45
  • Then how exactly does this go? Does the string get inserted into the stream and then all that goes into the buffer or idk how does it work? –  Sep 06 '21 at 21:58
-3

so,'std' is the namespace in which is stored everything found in the standard library , so basically you are saying "hey C++, go to 'std' storage and find cout command and run it" at least I know 'std' is this. and when you are saying using namespace std; you tell the compiler "take everything that's in the std namespace and dump it in the global namespace". I hope it helped you to understand.