Home C++ Iostream Functions showpos C++ showpos Manipulator 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.
std::cout << std::showpos << 42; // +42
std::cout << std::showpos << -42; // -42 Call What 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 ios_base& showpos(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << showpos. You never write this argument yourself.
Return Value Return Value Description ios_base&A reference to the same stream, so you can keep chaining << or >> after showpos.
C++ Examples 1. Integers 2. Doubles 3. Only decimal Example 1 — Print Integers with a Sign Positive numbers get a plus sign, negative ones already have a minus.
#include <iostream>
int main() {
std::cout << std::showpos << 42 << ' ' << -42 << ' ' << 0 << '\n';
return 0;
} How It Works showpos is put in before any number is printed.42 becomes +42. -42 stays -42, because it already shows a sign.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.
#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;
} today: +3.5 C
tomorrow: -1.25 C How It Works showpos stays on after it is put into cout, so both lines use it.3.5 is printed as +3.5.-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.
#include <iostream>
int main() {
std::cout << std::showpos;
std::cout << std::hex << 255 << ' ';
std::cout << std::dec << 255 << '\n';
return 0;
} How It Works std::hex switches the number base to 16, and 255 prints as ff.Hex output is treated as an unsigned value, so showpos does not add a plus sign. std::dec goes back to normal decimal numbers, and then 255 prints as +255.Frequently Asked Questions What does showpos do in C++? It prints a plus sign in front of non-negative numbers. Negative numbers keep their minus.
Does showpos work with doubles? Yes. It works for integers and floating-point numbers, for example +3.5.
Does zero get a plus sign? Yes. Zero is not negative, so with showpos it prints as +0.
Why does hex not show a plus? Hex and octal output are unsigned, so showpos does not apply.
How do I turn showpos off? Use std::noshowpos or cout.unsetf(std::ios::showpos).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion