1. Getting to know the std::cout command
You have just written your first program in C++ and printed the first message to the screen. Congratulations. Let's not stop here...
Any C++ program consists of commands (statements). Commands come in different kinds. The C++ language has commands for many situations. Each command describes a specific action. At the end of each command a semicolon is placed.
To print to the screen in C++ you typically use the output stream std::cout.
Examples of commands:
| Command | Description (what it does) |
|---|---|
|
Prints the number: |
|
Prints the text: |
|
Prints the text: |
Here we use the object std::cout and the operator << (read as “output to stream”). To finish the line, you usually add a newline: '\n'.
Case matters
In C++ case matters: it's important whether names are written in uppercase or lowercase. std::cout will work, while std::Cout will not. If you write something with the wrong letters, the C++ compiler won't recognize it and therefore won't execute it. So be careful.
Double quotes
If you want to print text to the screen, you need to mark it on both sides with double quotes.
A single quote looks like this ', and a double quote looks like this ". A double quote is not two single quotes: please don't confuse them.
The double quote is the one next to the Enter key. It usually also has the letter E on it.
2. Output without a newline
If you need to print text without moving to a new line, in C++ simply do not add '\n' at the end.
A command with '\n' prints the text and then moves the cursor to the next line, like pressing Enter. Thus the next output command will start writing on the new line.
By contrast, output without '\n' does not add an Enter after the output. Therefore text printed by several such commands will appear on the same line.
Example 1:
std::cout << "Amigo";
std::cout << "The" << '\n';
std::cout << "Best";
Will print:
AmigoThe
Best
Example 2:
std::cout << "Amigo";
std::cout << "The";
std::cout << "Best";
Will print:
AmigoTheBest
If you want to print text on the same line — don't add '\n'; if on multiple lines — add it. Simple.
3. Examining output via std::cout
If you know at least a little English, learning programming will be much easier. The names of many things in C++ are directly related to what they do.
For example cout is short for console output.
std::cout is one of the most common ways for a program to interact with a human. When a user types commands to the program on the keyboard and it responds by printing text to the screen, programmers say the program works with the console.
In other words, working with the console means printing text to the screen and reading input from the keyboard. So if in the future you hear that a program printed something to the console, it just means it printed text to the screen.
And we explicitly add '\n' (the newline character) to move to a new line in such examples.
Mini cheat‑sheet for output
When you're starting out, it's useful to keep a few "micro‑rules" in mind that often appear in tasks. Instead of a list let's make a small reminder table.
| Want to… | Write… | Note |
|---|---|---|
| Print text | |
string — in double quotes |
| Print a number | |
number — without quotes |
| Print a single character | |
character — in single quotes |
| Move to a new line | |
'\n' — the single newline character |
| Print a "complex" string made of parts | |
the chain of << prints sequentially |
All this may seem primitive, but these primitives are where people most often trip up: forgot a quote, mixed up ' and ", forgot the newline, didn't add a space.
4. Examples of printing various data
Let's play with console output. We'll start from the simplest and gradually complicate things:
Printing numbers
std::cout << 1001 << '\n';
std::cout << -15 << '\n';
Printing text
std::cout << "Programming is awesome!" << '\n';
std::cout << "Just kidding, now let's code." << '\n';
Printing multiple values at once
Trick: in C++ you can print several values in sequence by simply chaining them with the << operator:
std::cout << "My age: " << 28 << '\n';
Will print:
My age: 28
You can also print several pieces of text and numbers in a row:
std::cout << "Now the year is " << 2026 << " ." << '\n';
Will print:
Now the year is 2026.
5. Common mistakes when using std::cout
Young jedis often step on the same rakes. But so you don't become the hero of a book called "1001 C++ mistake", here is a list of common blunders and tips on how to avoid them.
Error 1. You forgot to close the string — lost the closing quote:
std::cout << "Oh, opened a quote and forgot to close);
The compiler will complain here (for example, something like missing terminating " character) — close your strings!
Error 2. Missing semicolon
In C++ each statement (command) usually ends with a semicolon. Sometimes it's forgotten:
std::cout << "No ; at the end" << '\n'
But C++ is unforgiving. Fix it — add the semicolon at the end of the line.
Error 3. Using incorrect quotes
Beginners sometimes use angled quotes or single quotes:
std::cout << ‘Error’ << '\n'; // wrong!
std::cout << 'Error' << '\n'; // also wrong!
In C++ single quotes '...' are for characters (for example, '\n' or 'A'), and for strings we always use standard double quotes — like this: ".
GO TO FULL VERSION