Skip to main content
Engineering LibreTexts

11.3: C++ Output Manipulators

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Manipulators in C++

    Manipulators are helping functions that can modify the input/output stream. It does not mean that we change the value of a variable, it only modifies the I/O stream using insertion (<<) and extraction (>>) operators.

    For example, if we want to print the hexadecimal value of 100 then we can print it as:

    cout << setbase(16) << 100

    the setbase() is a manipulator - it manipulates the way the data is displayed. In this case, it takes the ouptu, the number 100, and converts it to base 16 and dislays it as 64

    Types of Manipulators

    There are various types of manipulators:

    1. Manipulators without arguments: The most important manipulators defined by the IOStream library are provided below.
      • endl: It is defined in ostream. It is used to enter a new line and after entering a new line it flushes (i.e. it forces all the output written on the screen or in the file) the output stream.
      • ws: It is defined in istream and is used to ignore the whitespaces in the string sequence.
      • ends: It is also defined in ostream and it inserts a null character into the output stream. It typically works with std::ostrstream, when the associated output buffer needs to be null-terminated to be processed as a C string.
    2. flush: It is also defined in ostream and it flushes the output stream, i.e. it forces all the output written on the screen or in the file. Without flush, the output would be the same, but may not appear in real-time.

      Here is an example of how to use manipulators in your code

      #include <iostream> 
      #include <istream> 
      #include <sstream> 
      #include <string> 
      using namespace std; 
      int main() 
          istringstream str("         Programmer"); 
          string line; 
          // Ignore all the whitespace in string 
          // str before the first word. 
          getline(str >> std::ws, line); 
          // you can also write str>>ws 
          // After printing the output it will automatically 
          // write a new line in the output stream. 
          cout << line << endl; 
          // without flush, the output will be the same. 
          // flush simply forces the buffer to be sent in a buffered environment
          cout << "only a test" << flush; 
          // Use of ends Manipulator 
          cout << "\na"; 
          // NULL character will be added in the Output 
          cout << "b" << ends; 
          cout << "c" << endl; 
          return 0; 


      only a test
    3. Manipulators with Arguments: Some of the manipulators are used with the argument like setw (20), setfill (‘*’), and many more. These all are defined in the header file. If we want to use these manipulators then we must include this header file in our program.

      For Example, you can use following manipulators to set minimum width and fill the empty space with any character you want: std::cout << std::setw (6) << std::setfill (’*’);

      • Some important manipulators in <iomanip> are:
        1. setw (val): It is used to set the field width in output operations.
        2. setfill (c): It is used to fill the character ‘c’ on output stream.
        3. setprecision (val): It sets val as the new value for the precision of floating-point values.
        4. setbase(val): It is used to set the numeric base value for numeric values.
        5. setiosflags(flag): It is used to set the format flags specified by parameter mask.
        6. resetiosflags(m): It is used to reset the format flags specified by parameter mask.
      • Some important manipulators in <ios> are:
        1. showpos: It forces to show a positive sign on positive numbers.
        2. noshowpos: It forces not to write a positive sign on positive numbers.
        3. showbase: It indicates the numeric base of numeric values.
        4. uppercase: It forces uppercase letters for numeric values.
        5. nouppercase: It forces lowercase letters for numeric values.
        6. fixed: It uses decimal notation for floating-point values.
        7. scientific: It uses scientific floating-point notation.
        8. hex: Read and write hexadecimal values for integers and it works same as the setbase(16).
        9. dec: Read and write decimal values for integers i.e. setbase(10).
        10. oct: Read and write octal values for integers i.e. setbase(10).
        11. left: It adjusts output to the left.
        12. right: It adjusts output to the right.


    #include <iomanip> 
    #include <iostream> 
    using namespace std; 
    int main() 
        double A = 100; 
        double B = 2001.5251; 
        double C = 201455.2646; 
        // We can use setbase(16) here instead of hex 
        // turns on hexidecimal output, left justify 
        // numerical base, and forces lowercase
        cout << hex << left << showbase << nouppercase; 
        // actual printed part 
        cout << (long long)A << endl; 
        // We can use dec here instead of setbase(10) 
        // reset base back to base 10, right justified
        // and width to 15.
        // fills empty space with underline
        // show positive sign, precision to 2 places 
        cout << setbase(10) << right << setw(15) 
            << setfill('_') << showpos 
            << fixed << setprecision(2); 
        // actual printed part 
        cout << B << endl; 
        // scientific formatting, uppercase of the 'E'
        // precision to 9 places 
        cout << scientific << uppercase 
            << noshowpos << setprecision(9); 
        // actual printed part 
        cout << C << endl; 

    The first line of output is the value 100 - it is printed in hexadecimal, base 16, as specified in the code. the 0x signifies that this value is base 16.

    The second line of output is base 10, right justified, sets width to 15, fills empty space with underline, show positive sign, and precision to 2 places 

    The third line of output and the scientific formatting - we can tell because it has a lot of decimal places and a 'E+' near the end, precision is set to 9 places.


    Adapted from:
    "Manipulators in C++ with Examples" by PulkitDahiyaGeeks for Geeks is licensed under CC BY-SA 4.0 

    This page titled 11.3: C++ Output Manipulators is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?