Curriculum / C++ / Lesson 05

Input & Output in C++

C++ introduces stream-based I/O: cout for output and cin for input, both from <iostream>.

5 of 10
beginner 📖 4 min read 🎯 1 question

Input & Output

C++ introduces stream-based I/O: cout for output and cin for input, both from <iostream>. They use << and >> instead of format strings.

💡 Key insight cin and cout are type-safe — the compiler already knows the type of what you're printing or reading, unlike printf's format specifiers.

Syntax

syntax
cout << value;
cin >> variable;

Example

cin >> age reads a value typed by the user and stores it directly in age — no & needed, unlike C's scanf.

cpp
#include <iostream>
using namespace std;

int main() {
    int age;
    cout << "Enter your age: ";
    cin >> age;
    cout << "You are " << age << " years old." << endl;
    return 0;
}
outputEnter your age: 20 You are 20 years old.

Try it yourself

Copy this example into the C++ compiler and run it — try changing a value and see what happens.

Quick Quiz

1. Which operator is used to read input into a variable with cin?

Back to curriculum