C++ skipws Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, skipws makes the input operator >> skip whitespace (spaces, tabs, and new lines) before it reads a value. This is why you can type 12 7 with extra spaces or on two lines and still get two numbers.

It is on by default. You use skipws to switch the behaviour back on after noskipws. It comes from <ios>, which <iostream> includes.

This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).

Syntax

Put std::skipws into an input stream.

C++
std::cin >> std::skipws >> a >> b;     // spaces are skipped
std::cin >> std::noskipws >> c;        // next character, even a space
CallWhat it does
std::cin >> std::skipwsWhitespace in front of each value is skipped.
in >> std::skipws >> nReads n even if spaces come first.
in >> std::noskipwsThe opposite: whitespace is read like any other character.

Because it is the default, you almost never write skipws. It matters when you had turned it off with noskipws.

Library declarations
C++
ios_base& skipws(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cin >> skipws. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after skipws.

C++ Examples

Example 1 — Read a Number After Spaces

The spaces in front are skipped because skipws is on.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("   42");
    int n = 0;

    in >> n;

    std::cout << "n = " << n << '\n';
    return 0;
}

How It Works

  1. The text has three spaces before the number.
  2. >> skips the spaces first, since skipws is the default setting.
  3. Then it reads the digits, so n becomes 42.

Example 2 — Read Letters Separated by Spaces

Each >> jumps over the spaces and takes the next visible letter.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("a b c");
    char x = 0;
    char y = 0;
    char z = 0;

    in >> std::skipws >> x >> y >> z;

    std::cout << x << y << z << '\n';
    return 0;
}

How It Works

  1. The text a b c has spaces between the letters.
  2. With skipws each >> ignores the spaces in front of the next letter.
  3. x, y, and z hold a, b, and c, and they print without any spaces.

Example 3 — Turn skipws Back On After noskipws

Read a space on purpose, then go back to skipping.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("x y");
    char a = 0;
    char b = 0;
    char c = 0;

    in >> std::noskipws >> a >> b;
    in >> std::skipws >> c;

    std::cout << "a=" << a << " b=" << (b == ' ' ? "space" : "other") << " c=" << c << '\n';
    return 0;
}

How It Works

  1. noskipws stops the skipping, so a gets x and b gets the space character.
  2. skipws turns skipping back on. No whitespace is left in front of y, so it makes no difference here, but it matters for later reads.
  3. c reads y. The output shows that b really held a space.

Frequently Asked Questions

It makes >> skip whitespace characters (spaces, tabs, new lines) before it reads a value.
Yes. That is why cin >> a >> b works even when you type extra spaces between the numbers.
No. Only the formatted input operator >> skips whitespace. get() and getline() read every character.
Use cin >> std::noskipws >> c, or use cin.get(c). Then the space is returned like any other character.
skipws skips leading whitespace on input. noskipws does not, so whitespace is read as normal characters.

Next: unitbuf

Flush the stream after every output operation.

unitbuf 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.

9 people found this page helpful