Why does Std::Cout use <<?

Asked

Viewed 168 times

5

I am very beginner in language, maybe I am being rushed, but I would like to understand why std::cout and std::cin use respectively << and >> different from its "mother language" C, which uses the commands printf(""); and scanf("",%);

I have basic knowledge of C, but I saw that for the things I want to develop, C++ is more suitable, I program for pure fun.

Currently in C++ I can only write things on the console and use commands derived from C, such as if, for and etc.

But since I started seeing programmers coding in C++ it leaves me with a flea behind my ear.

1 answer

7


Because the language creators thought that this operator would be interesting to indicate "a piece of a stream". It was what visually gave some indication of being a stream, was available for this type of object and it seemed a good idea.

Note that only the cout and cin (in fact these guys are a cover on top of the real guys who manipulate the stream) have this operator help little, the types you use together need to have this operator also to be able to create the stream. The mechanism of stream is very complex and requires a lot of study.

The idea is that the result of this operator is always a formatted form of presentation of the object being used in stream, then the object provides the way it should appear, and need not be alone cout or cin, has several other possible ways of using this mechanism.

Although this seemed like a good idea at the time, 80’s, and in some cases still something useful, the mechanism proved problematic and limited, and in the most current version of the language is preferring a powerful data formatting mechanism, which I will not say is equal to that of C, because it’s so much better, but the way you remember it well. You can see more on documentation how to use it, will almost always be more interesting now and the stream will almost be a legacy.

Their correct names are stream Insertion Operator (<<) and stream Extraction Operator (>>), therefore even if the symbol is the same can not be confused with other existing operators, and that is one of the reasons that this is bad..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.