std::cout
A free lesson from Swipe Coding · C++, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
To display text on the screen (the "console") in C++, you use std::cout together with the << ("insertion") operator. Whatever you put after << gets sent to the console. To use it, your file needs #include <iostream> at the top — that line pulls in the standard input/output library.
#include <iostream>
int main() {
std::cout << "Hello, C++!" << std::endl;
return 0;
}What does this program print when you run it?
#include <iostream>
int main() {
std::cout << "Hello, C++!" << std::endl;
return 0;
}std::endl inserts a newline, just like pressing Enter, so the next output starts on a fresh line below it. It also does something extra: it "flushes" the output buffer, forcing any pending text to actually appear on screen right away. Most of the time you can also use the escape sequence "\n" instead — it inserts the same newline character, but without forcing a flush.
std::cout << "First line" << std::endl; std::cout << "Second line\n"; // Output: // First line // Second line
The stream manipulator ____ inserts a newline and also forces the output buffer to flush immediately.
std:: means "this comes from the std namespace" — the part of C++'s standard library where things like cout and endl live. You will sometimes see files that start with using namespace std; so they can write cout instead of std::cout everywhere. This course writes std:: explicitly, since it makes it obvious exactly where each name comes from.
You must write using namespace std; before you are allowed to use std::cout.
You now know how to print plain text with std::cout. Next, you will learn about escape characters — special codes that let you insert things like line breaks, tabs, and quotation marks directly inside a piece of text.
Keep your streak going in Swipe Coding
Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →