Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::dec — decimal integer formatting for streams

std::dec selects base 10 for formatted integer output and input in a C++ stream. The change remains active for subsequent operations until the stream's numeric base setting is changed again.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <ios>
std::ios_base& std::dec(std::ios_base& str);
out << std::dec << value;
in >> std::dec >> value;

std::dec is a stream manipulator declared in <ios>. Use it without parentheses: out << std::dec << value for output or in >> std::dec >> value for input. It selects base 10 for formatted integer operations; it does not change the value stored in a variable.

Specifically, it calls str.setf(std::ios_base::dec, std::ios_base::basefield) and returns str by reference. This replaces the previous base setting, such as one selected by std::hex or std::oct, without resetting unrelated formatting flags. Its effect lasts until the base setting changes again, rather than applying to just one operation.

std::dec does not set the number of decimal places or control floating-point formatting. The manipulator itself neither writes characters nor reads a number: it changes stream state used by the next formatted operation. For input, it explicitly selects base 10 rather than automatic prefix-based base detection.

The example first prints 42 in hexadecimal as 2a, then restores decimal output for 42 and 43. A separate input stream reads two decimal integers. This entry targets a C++23 configuration; the current working draft supplies the description, not evidence of behavior in a production runtime.

Examples

#include <ios>
#include <iostream>
#include <sstream>

int main() {
    std::cout << std::hex << 42 << ' '
              << std::dec << 42 << ' ' << 43 << '\n';

    std::istringstream input("42 43");
    int a = 0;
    int b = 0;
    if (!(input >> std::dec >> a >> b)) {
        return 1;
    }
    std::cout << a << ' ' << b << '\n';
}

Result

2a 42 43
42 43

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources