Output in C++

Text Output

Notice how we output "Hello World!"? In fact, you can output anything you like by changing the words in the double quotes! It's that easy!

Example:
cout << "Whatever, just text.";

Numbers Output

Replacing the double quotes with a number results in the number being outputted. This works with not only integers, but also decimals. Note that it will automatically truncate the number until six-seven digits remain. This is due to the method that C++ stores decimals and we will cover that later.

Example:
cout << 123456789.123456789;
outputs
1.23457e+08
, meaning \(1.23457 \times 10^8\).
cout << 0.123456789;
outputs
0.123457
.

Escape Characters

What happens of you try to output a newline? C++ ignores whitespace, so

cout << "
";
does not work. To include a newline, you need to escape it with a backslash.
cout << "\n";
outputs a newline. This is called "escape characters", meaning the character combination changes its meaning. A few common ones include:
Character Meaning
\n Newline
\t Tab
\r Carriage Return
\v Vertical Tab
\f Form Feed
\b Backspace
\a Alert
\\ Backslash
\" Double quote
\' Single quote
\% Percent sign
For more, please visit https://en.cppreference.com/w/cpp/language/escape

There is one more trick of outputting these characters: Raw String Literals. Introduced in C++11 (a version published in the year 2011), these string literals ignore no whitespace and perform no escaping -- they are outputted just as they are. They are used like so:

R"(STRING_CONTENT)"
.
Example:
cout << R"(this is a \n raw string literal)";
will output
this is a \n raw string literal

Now head on to the next page to learn about variables!