Why is this operator needed?

+1 John W · January 24, 2015
Can someone please explain to me why you need the "stream insertion operator" that I underlined in the function below? This code appears about halfway through video 10 of the C++ tutorials. 

void printCrap(int x){

    cout << "Buckys's favorite number is " <<  x  << endl;


Why can't you write it as:

void printCrap(int x){

    cout << "Buckys's favorite number is "  x  << endl;


I don't understand why you need the operator between the x variable and the quotes. 

Post a Reply


Oldest  Newest  Rating
+2 Cardinal Coog · January 25, 2015
Basically, it's almost like you're using "cout" three times on that line. Once to display the quotes, "Bucky's favorite number is: ". The second time is to actually display the favourite number variable X. And, then a third time to end the line.
+1 Cardinal Coog · January 25, 2015
That's the basic syntax for using "cout". The first set of arrows is to print out what is in quotes. Then, you have the arrows again so that you are using "cout" again to display the variable x. Finally, you repeat the arrows again for the "endl;" 

Make sense? If not, let me know and I'll try to explain it in a different way.
0 John W · January 25, 2015
Ok I think I get what you are saying Cardinal and Krootushas. I'll trust you Mathias that it will make more sense later on. Thanks for the help guys.
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator