01 / 21
<< "Hi"

Formatted Output
with std::cout

Learn how to display text and data to the console using the C++ insertion operator, escape sequences, and stream manipulators.

C++

What is std::cout?

std::cout stands for Character Output. It is a predefined object in C++ that represents the standard output stream, typically your terminal or console window. It lives inside the <iostream> header and the std namespace.

The Insertion Operator <<

std::cout << "Hello" << age stream object string literal variable insertion insertion

The << operator inserts data into the output stream. You can chain multiple << operators to send several items in one statement. Data flows left to right.

Why Formatting Matters

Readability
Well-formatted output makes programs easier to understand and debug.
Professionalism
Clean console output looks polished and communicates results clearly.
Accuracy
Controlling decimal places, alignment, and spacing prevents misinterpretation.

The Basic Pattern

#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
Output
Hello, World!

Escape Sequences

Escape sequences are special character combinations starting with a backslash \. They let you insert characters that cannot be typed directly into a string literal.

Sequence Meaning Example Output
\nNew lineLine 1
Line 2
\tHorizontal tabCol1    Col2
\\BackslashC:\path
\"Double quoteShe said "hi"

std::endl vs \n

std::endl
Inserts a newline and flushes the output buffer. Guarantees text appears immediately but is slightly slower.
\n
Inserts a newline only. Faster because it does not force a buffer flush. Preferred for most use cases.

Stream Manipulators

The <iomanip> header provides manipulators that control how data is formatted. They are inserted into the stream like regular data and change the formatting for all subsequent output.

1
std::setw(n): Sets the minimum field width for the next item only.
2
std::setprecision(n): Controls how many digits are displayed for floating-point numbers.
3
std::fixed: Forces decimal notation (e.g., 3.14 instead of 3.14159e+00).
4
std::left / std::right: Aligns output within the field width.

How to Format Output

1
Include headers: Add <iostream> for cout. Add <iomanip> if you need setw, setprecision, or fixed.
2
Choose your data: Decide what you are printing: strings, integers, or floating-point numbers.
3
Apply manipulators: Insert std::fixed and std::setprecision() before the value if formatting decimals.
4
Chain and end: Use << to chain items. End with \n or std::endl.
5
Test your output: Compile and run. Verify the console matches your expected format.

Example 1: Mixing Text and Variables

#include <iostream> int main() { int age = 16; std::cout << "I am " << age << " years old.\n"; return 0; }
Output
I am 16 years old.

Example 2: Decimal Precision

#include <iostream> #include <iomanip> int main() { double pi = 3.14159265358979; std::cout << std::fixed << std::setprecision(2) << "Pi: " << pi << "\n"; return 0; }
Output
Pi: 3.14

Example 3: Field Width and Alignment

#include <iostream> #include <iomanip> int main() { std::cout << std::left << std::setw(10) << "Name" << std::setw(6) << "Age" << "\n"; std::cout << std::setw(10) << "Alice" << std::setw(6) << 17 << "\n"; std::cout << std::setw(10) << "Bob" << std::setw(6) << 16 << "\n"; return 0; }
Output
Name Age Alice 17 Bob 16

Chaining Multiple Items

int x = 5, y = 10; std::cout << "Sum of " << x << " and " << y << " is " << (x + y) << "\n";
Output
Sum of 5 and 10 is 15

Manipulator Persistence

setw(10) one-shot resets fixed sticky setprecision sticky Sticky manipulators remain active until explicitly changed
One-shot
setw() resets after each item. You must call it before every value you want padded.
Sticky
fixed, setprecision, left, right all persist until you explicitly change them.

Common Mistakes

✕ Forgetting Spaces
Writing "Hello"<<name without a trailing space in the string. Output: HelloAlice instead of Hello Alice.
✕ Missing <iomanip>
Using setw or setprecision without including the <iomanip> header causes a compile error.
✕ Expecting setw to Stick
Using setw(10) once and expecting it to apply to all subsequent values. It only applies to the next item.

Manipulator Summary

Manipulator Header Persistence Purpose
std::endl <iostream> One-shot Newline + flush
std::setw(n) <iomanip> One-shot Field width
std::setprecision(n) <iomanip> Sticky Decimal digits
std::fixed <iomanip> Sticky Decimal notation
std::left / std::right <iomanip> Sticky Alignment
std::setfill(c) <iomanip> Sticky Fill character

Question 1 of 5

What does the << operator do in the context of std::cout?

Question 2 of 5

What will the following code output?

std::cout << "A\tB\nC";

Question 3 of 5

Which header must you include to use std::setprecision?

Question 4 of 5

What happens if you call std::setw(10) once and then output three values without calling setw again?

Question 5 of 5

What will this code output?

double val = 7.5; std::cout << std::fixed << std::setprecision(3) << val << "\n";