C++ dec Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, dec sets the number base of a stream to 10 (decimal). It is the default, so it matters when you want to go back after hex or oct.

The setting works for both output and input: cout << dec << n prints in base 10, and cin >> dec >> n reads digits as base 10. 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::dec into the stream.

C++
std::cout << std::hex << 255;      // ff
std::cout << std::dec << 255;      // 255
CallWhat it does
std::cout << std::decPrints integers in base 10.
std::cin >> std::dec >> nReads n as a normal decimal number.
std::cout << std::hex << a << std::dec << bHex for a, decimal for b.

Remember to switch back. A forgotten hex makes all later numbers print in base 16.

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

Parameters

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

C++ Examples

Example 1 — Go Back to Decimal After hex

The same number shows two ways.

C++
#include <iostream>

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

How It Works

  1. std::hex makes 255 print as ff.
  2. std::dec sets the base back to 10.
  3. The second line prints 255 in the usual form.

Example 2 — Read in Hex, Print in Decimal

in >> std::hex reads the digits as base 16. Then dec prints the value as a normal number.

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

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

    in >> std::hex >> n;

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

How It Works

  1. With hex, the digits 255 are read as a base 16 number, not as two hundred fifty-five.
  2. In base 16, 255 means 2 * 256 + 5 * 16 + 5, which is 597.
  3. n is a plain int and has no base. dec makes cout print it as 597.

Example 3 — Negative Numbers in Hex and Decimal

In hex a negative number shows its unsigned form. In decimal it keeps the minus sign.

C++
#include <iostream>

int main() {
    int n = -1;

    std::cout << std::hex << n << '\n';
    std::cout << std::dec << n << '\n';
    return 0;
}

How It Works

  1. -1 is stored with all bits set to 1. On most computers an int has 32 bits.
  2. In hex, those 32 bits are eight digits f, so ffffffff is printed.
  3. dec shows the signed meaning again, which is -1.

Frequently Asked Questions

It sets the number base of a stream to decimal (base 10) for both input and output.
Yes. Streams start in decimal. You need dec to go back after hex or oct.
Yes. cin >> dec >> n reads digits as base 10.
In hex the number is printed as unsigned, and -1 has all 32 bits set. Switch to dec to see -1.
No. The base manipulators only change integer types.

Next: endl

End the line and flush the output.

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

11 people found this page helpful