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?
 
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
 
Sourodip KunduAuthor Commented:
Thank you, problem solved
0
 
phoffricCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.