C++ std::ostream::put() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, put(c) writes exactly one character to an output stream. It does no formatting. Whatever character you give, that character appears.

The result is always a character, even if you pass a number. That makes it different from cout << number, which prints digits. Include <iostream> to use it.

This is a member function of std::ostream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call put on an output stream and pass one character.

C++
std::cout.put('A');
std::cout.put('B').put('\n');     // calls can be chained
CallWhat it does
std::cout.put('A')Writes the letter A.
std::cout.put(65)Writes A too, because the number 65 is the code of A.
out.put('x').put('y')Chains two writes. put() returns the stream.

For normal text use <<. put() is useful when you build output one character at a time.

Library declarations
C++
ostream& put(char_type ch);

Parameters

ParameterDescription
chRequired. The character to write. A number is converted to the character with that code.

Return Value

Return ValueDescription
ostream&A reference to the stream. If the write failed, badbit is set.

C++ Examples

Example 1 — Write Characters with put()

Because put() returns the stream, you can chain calls.

C++
#include <iostream>

int main() {
    std::cout.put('C').put('+').put('+').put('\n');
    return 0;
}

How It Works

  1. std::cout.put('C') writes C and returns std::cout.
  2. The returned stream is used for the next put(), so + and + follow.
  3. The last put('\n') writes a new line character, which moves the cursor to the next line.

Example 2 — Write a String One Character at a Time

Loop over a string and send each character with put(). Here each letter is made uppercase.

C++
#include <cctype>
#include <iostream>
#include <string>

int main() {
    std::string word = "hello";

    for (char c : word) {
        std::cout.put(static_cast<char>(std::toupper(c)));
    }
    std::cout.put('\n');
    return 0;
}

How It Works

  1. The for loop visits every character of word.
  2. std::toupper(c) gives the uppercase version, and put() writes just that one character.
  3. After the loop, one more put('\n') ends the line.

Example 3 — put() with a Number

A number given to put() becomes a character. Compare it with <<.

C++
#include <iostream>

int main() {
    std::cout << 65 << '\n';
    std::cout.put(65);
    std::cout.put('\n');
    return 0;
}

How It Works

  1. std::cout << 65 is formatted output. It writes the two digits 6 and 5.
  2. std::cout.put(65) is raw output. The number is treated as a character code, and code 65 is the letter A.
  3. This is the main thing to remember: << formats values, while put() writes one character.

Frequently Asked Questions

It writes one character to the output stream with no formatting.
For a char they give the same output. The difference is with numbers: put(65) writes the letter A, while cout << 65 writes the digits 65.
Yes. put() returns a reference to the stream, so cout.put('A').put('B') works.
The stream sets badbit. You can check it with cout.bad() or by testing the stream in an if condition.
Use << for almost everything. put() is helpful only when you are working one character at a time.

Next: putback()

Push a character back so it is read again.

putback() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful