CPP C++ Manipulators

C++ Manipulators

  • Manipulators are operators used in C++ for formatting output. The data is manipulated by the programmer’s choice of display.
  • In this C++ tutorial, you will learn what a manipulator is, endl manipulator, setw manipulator, setfill manipulator and setprecision manipulator are all explained along with syntax and examples.

endl Manipulator:

  • This manipulator has the same functionality as the ‘n’ newline character.

For example:

1. cout << "Exforsys" << endl; 
2. cout << "Training";

setw Manipulator:

  • This manipulator sets the minimum field width on output.

  • Here setw causes the number or string that follows it to be printed within a field of x characters wide and x is the argument set in setw manipulator.
  • The header file that must be included while using setw manipulator is .


#include <iostream>
#include <iomanip> 
void main( )
int x1=123,x2= 234, x3=789;
cout << setw(8) << "Exforsys" << setw(20) << "Values" << endl
<< setw(8) << "test123" << setw(20)<< x1 << endl
<< setw(8) << "exam234" << setw(20)<< x2 << endl
<< setw(8) << "result789" << setw(20)<< x3 << endl;


test                  123
exam                  234
result                  789


setfill Manipulator:

  • This is used after setw manipulator.
  • If a value does not entirely fill a field, then the character specified in the setfill argument of the manipulator is used for filling the fields.


#include <iostream>
#include <iomanip>
void main()
cout << setw(15) << setfill('*') << 99 << 97 << endl;




setprecision Manipulator:

  • The setprecision Manipulator is used with floating point numbers.
  • It is used to set the number of digits printed to the right of the decimal point.
  • This may be used in two forms:
  1. fixed
  2. scientific
  • These two forms are used when the keywords fixed or scientific are appropriately used before the setprecision manipulator.
  • The keyword fixed before the setprecision manipulator prints the floating point number in fixed notation.
  • The keyword scientific, before the setprecision manipulator, prints the floating point number in scientific notation.


#include <iostream>
#include <iomanip>
void main( )
float x = 0.1;
cout << fixed << setprecision(3) << x << endl;
cout << scientific << x << endl;




  • The first cout statement contains fixed notation and the setprecision contains argument 3.
  • This means that three digits after the decimal point and in fixed notation will output the first cout statement as 0.100. The second cout produces the output in scientific notation.
  • The default value is used since no setprecision value is provided.

Share This Page to Download:

Share to Download

Share This Page on:

Ask Question