← Back to C++ Course | Chapter 2: Input & Output | Lesson 1 of 8

C++ cin & cout

cout shows things on the screen and cin reads what the person types, like speaking and listening. Together they let your program have a conversation.
Syntax
cpp
std::cout << value << std::endl;

std::cin >> variable_name;

What is cout?

cout is the standard output stream, used with the insertion operator << to send text, numbers, and variable values to the console.

It's the tool you reach for any time your program needs to display something to the person running it, from a simple greeting to a computed result.

Example: What is cout?

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Hello, world!" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

What is cin?

cin is the standard input stream, paired with the extraction operator >> to read whatever the user types and store it in a variable.

It automatically converts the typed text into the variable's type, so cin >> age where age is an int will parse the digits the user entered as a number.

Example: What is cin?

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("25"); // stand-in for real std::cin input
	int age;
	cin >> age;
	std::cout << "Age: " << age << std::endl;
	return 0;
}

Chaining Output with cout

Chaining several << operators in one statement, like cout << "Sum: " << total << endl;, lets you combine labels and values into one readable line instead of writing separate print statements.

This cascading style is idiomatic C++ and keeps output code compact.

Example: Chaining Output with cout

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare total and set it to 42
	int total = 42;
	// Print to the console with cout
	std::cout << "Sum: " << total << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Multiple Inputs with cin

You can chain >> the same way to read several values in one line, such as cin >> width >> height;, and C++ automatically treats spaces, tabs, and newlines as separators between values.

This means the user can type 5 10 or press Enter between numbers and either way works identically.

Example: Multiple Inputs with cin

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("5 10"); // stand-in for "cin >> width >> height;"
	int width, height;
	cin >> width >> height;
	std::cout << width << " " << height << std::endl;
	return 0;
}

cout with endl and Newline

endl and \n both move output to a new line, but endl additionally flushes the output buffer, forcing any pending data to actually be written to the screen immediately.

\n is faster in tight loops since it skips that flush, so many C++ programmers prefer \n for routine output and reserve endl for moments where an immediate flush genuinely matters, like right before a crash or a long-running operation.

Example: cout with endl and Newline

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Flushed immediately" << std::endl;
	// Print to the console with cout
	std::cout << "Faster in tight loops" << "\n";
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using the wrong arrow direction, like cin << x; or cout >> x;; input uses >> and output uses <<.
  2. Reading with cin >> name and expecting a full sentence, when it stops at the first whitespace.
  3. Forgetting std:: or using namespace std;, so cin and cout are not found.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.