codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

76 / 87 topics
76<iostream> Reference77<fstream> Reference78<cmath> Reference79<string> Reference80<cstring> Reference81<ctime> Reference82<vector> Reference83<algorithm> Reference
Tutorials/C++ Programming/&lt;iostream&gt; Reference
⚡C++ Programming

&lt;iostream&gt; Reference

Updated 2026-05-12
30 min read

<iostream> Reference

The &lt;iostream&gt; library in C++ is essential for handling input and output operations. Whether you're reading from the keyboard, writing to the screen, or logging errors, understanding how to use this library effectively is crucial for any C++ programmer.

In this tutorial, we'll explore various components of the &lt;iostream&gt; library, including cout, cin, cerr, clog, endl, flush, getline, put, and get. We'll also delve into formatted I/O using the &lt;iomanip&gt; library. By the end of this tutorial, you'll have a solid understanding of how to perform input and output operations in C++.

Core Content

1. Basic Output: cout

cout is used for standard output, typically the screen. It's part of the std namespace and is commonly used with the insertion operator (<<).

basic_output.cpp
1#include <iostream>
2
3int main() {
4 std::cout << "Hello, World!" << std::endl;
5 return 0;
6}
Output
Hello, World!

2. Basic Input: cin

cin is used for standard input, typically from the keyboard. It's also part of the std namespace and is commonly used with the extraction operator (>>).

basic_input.cpp
1#include <iostream>
2
3int main() {
4 int number;
5 std::cout << "Enter a number: ";
6 std::cin >> number;
7 std::cout << "You entered: " << number << std::endl;
8 return 0;
9}
Output
Enter a number: 42
You entered: 42

3. Error Output: cerr

cerr is used for error messages. Unlike cout, it does not use buffering, ensuring that error messages are displayed immediately.

error_output.cpp
1#include <iostream>
2
3int main() {
4 std::cerr << "This is an error message!" << std::endl;
5 return 0;
6}
Output
This is an error message!

4. Logging Output: clog

clog is similar to cout, but it also does not use buffering, making it suitable for logging purposes.

logging_output.cpp
1#include <iostream>
2
3int main() {
4 std::clog << "This is a log message!" << std::endl;
5 return 0;
6}
Output
This is a log message!

5. Manipulators: endl and flush

endl inserts a newline character and flushes the output buffer, ensuring that all data is written to the destination.

endl_flush.cpp
1#include <iostream>
2
3int main() {
4 std::cout << "Line 1";
5 std::cout << std::endl;
6 std::cout << "Line 2";
7 return 0;
8}
Output
Line 1
Line 2

flush also flushes the output buffer without inserting a newline character.

flush.cpp
1#include <iostream>
2
3int main() {
4 std::cout << "Part 1";
5 std::cout.flush();
6 std::cout << " Part 2";
7 return 0;
8}
Output
Part 1 Part 2

6. Reading a Line: getline

getline reads an entire line of input, including spaces, until it encounters a newline character.

getline.cpp
1#include <iostream>
2#include <string>
3
4int main() {
5 std::string name;
6 std::cout << "Enter your full name: ";
7 std::getline(std::cin, name);
8 std::cout << "Hello, " << name << "!" << std::endl;
9 return 0;
10}
Output
Enter your full name: John Doe
Hello, John Doe!

7. Writing a Single Character: put

put writes a single character to the output stream.

put.cpp
1#include <iostream>
2
3int main() {
4 std::cout.put('H');
5 std::cout.put('e');
6 std::cout.put('l');
7 std::cout.put('l');
8 std::cout.put('o');
9 std::cout.put('!');
10 return 0;
11}
Output
Hello!

8. Reading a Single Character: get

get reads a single character from the input stream.

get.cpp
1#include <iostream>
2
3int main() {
4 char ch;
5 std::cout << "Enter a character: ";
6 std::cin.get(ch);
7 std::cout << "You entered: " << ch << std::endl;
8 return 0;
9}
Output
Enter a character: A
You entered: A

9. Formatted I/O with &lt;iomanip&gt;

The &lt;iomanip&gt; library provides manipulators for formatting input and output operations.

a. Setting Width

setw sets the field width for subsequent output operations.

setw.cpp
1#include <iostream>
2#include <iomanip>
3
4int main() {
5 int num = 42;
6 std::cout << "Number: " << std::setw(10) << num << std::endl;
7 return 0;
8}
Output
Number:         42

b. Setting Precision

setprecision sets the number of digits to be displayed in floating-point numbers.

setprecision.cpp
1#include <iostream>
2#include <iomanip>
3
4int main() {
5 double pi = 3.141592653589793;
6 std::cout << "PI: " << std::setprecision(5) << pi << std::endl;
7 return 0;
8}
Output
PI: 3.1416

c. Fixed and Scientific Notation

fixed and scientific control the notation used for floating-point numbers.

fixed_scientific.cpp
1#include <iostream>
2#include <iomanip>
3
4int main() {
5 double pi = 3.141592653589793;
6 std::cout << "Fixed: " << std::fixed << pi << std::endl;
7 std::cout << "Scientific: " << std::scientific << pi << std::endl;
8 return 0;
9}
Output
Fixed: 3.141593
Scientific: 3.141593e+00

Practical Example

Let's create a complete program that demonstrates various input and output operations, including formatted I/O.

practical_example.cpp
1#include <iostream>
2#include <iomanip>
3#include <string>
4
5int main() {
6 std::string name;
7 int age;
8 double height;
9
10 std::cout << "Enter your full name: ";
11 std::getline(std::cin, name);
12
13 std::cout << "Enter your age: ";
14 std::cin >> age;
15
16 std::cout << "Enter your height (in meters): ";
17 std::cin >> height;
18
19 std::cout << "
20--- User Information ---
21";
22 std::cout << "Name: " << name << std::endl;
23 std::cout << "Age: " << age << std::endl;
24 std::cout << "Height: " << std::fixed << std::setprecision(2) << height << " meters" << std::endl;
25
26 return 0;
27}

Expected Output

Enter your full name: John Doe Enter your age: 30 Enter your height (in meters): 1.75

--- User Information --- Name: John Doe Age: 30 Height: 1.75 meters

Summary

ConceptDescription
coutStandard output stream for displaying text on the screen.
cinStandard input stream for reading data from the keyboard.
cerrError message stream, not buffered.
clogLogging stream, not buffered.
endlInserts a newline and flushes the output buffer.
flushFlushes the output buffer without inserting a newline.
getlineReads an entire line of input, including spaces.
putWrites a single character to the output stream.
getReads a single character from the input stream.
&lt;iomanip&gt;Provides manipulators for formatting I/O operations.
setwSets the field width for subsequent output operations.
setprecisionSets the number of digits to be displayed in floating-point numbers.
fixedForces floating-point numbers to use fixed notation.
scientificForces floating-point numbers to use scientific notation.

What's Next?

Now that you have a comprehensive understanding of input and output operations using the &lt;iostream&gt; library, it's time to explore file I/O with the &lt;fstream&gt; library. The next tutorial will cover how to read from and write to files in C++. Stay tuned!


PreviousFunctorsNext <fstream> Reference

Recommended Gear

Functors<fstream> Reference