C++ showpos Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, showpos makes a stream print a + in front of every non-negative number. Negative numbers keep their - sign as always.

It works for integers and for floating-point numbers, which is helpful for changes, scores, and temperatures. 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::showpos into the stream before the numbers.

C++
std::cout << std::showpos << 42;     // +42
std::cout << std::showpos << -42;    // -42
CallWhat it does
std::cout << std::showposTurns the plus sign on.
std::cout << std::showpos << 42Prints +42.
std::cout << std::showpos << 3.14Prints +3.14.

Zero also gets a plus sign: it prints as +0.

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

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << showpos. 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 showpos.

C++ Examples

Example 1 — Print Integers with a Sign

Positive numbers get a plus sign, negative ones already have a minus.

C++
#include <iostream>

int main() {
    std::cout << std::showpos << 42 << ' ' << -42 << ' ' << 0 << '\n';
    return 0;
}

How It Works

  1. showpos is put in before any number is printed.
  2. 42 becomes +42. -42 stays -42, because it already shows a sign.
  3. Zero is not negative, so it also gets a plus sign.

Example 2 — showpos with Decimal Numbers

It also works for floating-point numbers. Here it shows a temperature change.

C++
#include <iostream>

int main() {
    double change = 3.5;
    double drop = -1.25;

    std::cout << std::showpos;
    std::cout << "today: " << change << " C\n";
    std::cout << "tomorrow: " << drop << " C\n";
    return 0;
}

How It Works

  1. showpos stays on after it is put into cout, so both lines use it.
  2. 3.5 is printed as +3.5.
  3. -1.25 is negative and keeps its own sign. Reports of changes are easy to read when positive and negative are both signed.

Example 3 — showpos Applies to Decimal Output Only

Hex numbers are printed as unsigned values, so no plus sign appears.

C++
#include <iostream>

int main() {
    std::cout << std::showpos;
    std::cout << std::hex << 255 << ' ';
    std::cout << std::dec << 255 << '\n';
    return 0;
}

How It Works

  1. std::hex switches the number base to 16, and 255 prints as ff.
  2. Hex output is treated as an unsigned value, so showpos does not add a plus sign.
  3. std::dec goes back to normal decimal numbers, and then 255 prints as +255.

Frequently Asked Questions

It prints a plus sign in front of non-negative numbers. Negative numbers keep their minus.
Yes. It works for integers and floating-point numbers, for example +3.5.
Yes. Zero is not negative, so with showpos it prints as +0.
Hex and octal output are unsigned, so showpos does not apply.
Use std::noshowpos or cout.unsetf(std::ios::showpos).

Next: skipws

Skip leading spaces and new lines when reading.

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

8 people found this page helpful