Link to home
Start Free TrialLog in
Avatar of Sourodip Kundu
Sourodip Kundu

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sourodip Kundu
Sourodip Kundu

ASKER

Thank you, problem solved
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.