We print to output streams using the << operator:
int i; char b[30]; cout << i; cout << b; Like >>, the << operator is "aware" of the type that is being output. When given a char array, cout treats the array as a C-style string (a null-terminated array of char)..
The meaning of >> and << differ based on the types of their arguments. In the example above, the << operator will invoke a different output routine based on the type of the arguments.
We therefore say the operator is overloaded---the same name can mean different things.
Like any other binary operator, >> takes two operands and returns a value. When the operator >> is used with an istream as the left argument, the return value is of type istream &---that is, a "reference to an istream object".
This reference will point to the original istream object. Therefore, what happens when we evaluate the following expression?
We first evaluate "cin >> b", which returns a reference to cin. We then evaluate the result of using this istream object with ">> i", which results in the output of i, and so on.
The precedence of the << and
>> operators is lower than that of most arithmetic
operations. Therefore, in the following expression:
we evaluate the "i+2" expression before applying the << operation.
By the way, notice that the following two operations are not symmetric:
The former outputs everything in the array until the first null character, whereas the latter will read into the array until the first whitespace character.