Input and Output

To be useful, a program must communicate with the world and save calculated results on disk. Most C++ systems do not automatically show the result of every expression the way UnderC does. C++ programs use the iostreams library, which defines input and output “streams”.

Writing to cout

With C++, a program can direct values and text to output by using the insertion operator (<<), acting on cout (pronounced “see-out”), which is a special variable that represents the standard output stream. (Bjarne Stroustrup suggests we call << “put to.”) The following example uses << to output a string literal and an integer. Note that the items follow each other on output with no extra spaces:

;> cout << "nn = " << nn << endl; nn = 2 (ostream&) ...

Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.