Home C++ Iostream Functions dec C++ dec Manipulator 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.
std::cout << std::hex << 255; // ff
std::cout << std::dec << 255; // 255 Call What 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 ios_base& dec(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << dec. 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 dec.
Important
The base manipulators dec, hex, and oct stay on until changed. Only integers are affected: double values are not printed in hex. Negative numbers are shown with a minus only in decimal. In hex they print in their unsigned form.
C++ Examples 1. Switch back 2. Read hex, print dec 3. Negative numbers Example 1 — Go Back to Decimal After hex The same number shows two ways.
#include <iostream>
int main() {
std::cout << std::hex << 255 << '\n';
std::cout << std::dec << 255 << '\n';
return 0;
} How It Works std::hex makes 255 print as ff.std::dec sets the base back to 10.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.
#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 With hex, the digits 255 are read as a base 16 number, not as two hundred fifty-five. In base 16, 255 means 2 * 256 + 5 * 16 + 5, which is 597. 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.
#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 is stored with all bits set to 1. On most computers an int has 32 bits.In hex, those 32 bits are eight digits f, so ffffffff is printed. dec shows the signed meaning again, which is -1.Frequently Asked Questions What does dec do in C++? It sets the number base of a stream to decimal (base 10) for both input and output.
Is dec the default? Yes. Streams start in decimal. You need dec to go back after hex or oct.
Does dec work for input? Yes. cin >> dec >> n reads digits as base 10.
Why do I see ffffffff for -1 in hex? In hex the number is printed as unsigned, and -1 has all 32 bits set. Switch to dec to see -1.
Does dec affect double values? No. The base manipulators only change integer types.
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.
11 people found this page helpful
Helpful Share Copy link Suggestion