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.
