Why we need << operator twice to print the value in c++

As per c++ primer book the description of writing stream opearator(<<) is-

std::cout << "Enter two numbers:" << std::endl;

 The << operator takes two operands: The left-hand operand must be an ostream object; the right-hand operand is a value to print. The operator writes the given value on the given ostream. The result of the output operator is its left-hand operand. That is, the result is the ostream on which we wrote the given value. Our output statement uses the << operator twice. Because the operator returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second. As a result, we can chain together output requests.

I can't understand the above description. That is why we need two operators because one << operator is enough to print the values?
Sourodip KunduAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
It's explaining how to chain.

There are actually 2 statements in that piece of code:

cout << "Enter Two numbers";  //result is the stream cout stream.
cout << endl; // result is still the stream.

Open in new window


The result of the first line of code is the cout stream again, which allows you to chain the endl on top of it in one statement.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sourodip KunduAuthor Commented:
Thank you, problem solved
0
phoffric\Commented:
Will try to clarify a little more.
 Consider the expression: 3+ 1 +4
 You know that this expression equals 8.
 You understand this intuitively. Let's look how a compiler thinks of it.
 The + Is a binary operator taking a left right operand.
 The compiler breaks it down like this:
(3+ 1) +4
 To the compiler 3+ 1 is more than just two numbers added together. It also recognizes that these two numbers are integers and the binary operator of these two integers is going to return a number of the type integer. So enough expression in parentheses returns a value and type ; namely the number 4 and the type integer . This first expression then becomes the left upper and of another addition, 4+4. Compiler now two integers and returns the number eight along with its type which is an integer .

 The above is a direct analogy of what you have.
std::cout << "Enter two numbers:" << std::endl;
 This statement is an expression having two binary operators that are both the same ; namely, >>.
 And as above the compiler will consider the first binary operator as follows :
(std::cout << "Enter two numbers:") << std::endl;
 Expression and parentheses the binary operator to operands with Sealth in the left upper end and the string being the right operation. And you know this results in the string being output to your console. The compiler is going to keep track of what type this expression is returning . And the rule is as you wrote OP as follows :

Because the operator returns its left-hand operand,
Focusing on the expression within the parentheses , that expression returns the left hand operand which is cout of type ostream. This object, cout , and the ostream type become the left-hand operator of the second << operator; and the right-hand side of the second expression is endl.

 Justise you might say that the 2+ in the first numerical example might be considered changing, we also have a changing of operators In your expression.

 I hope this analogy helps.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.