Manipulators are used to manage the output stream and to modify the user output. Different manipulators are used to set different output formats.

Manipulators are operator that are used to format the data display. The most commonly manipulators are endl and setw.

To use various manipulators in C++ program <iomanip.h> header file is compulsory to include.

List of Manipulators

  1. endl
  2. setw
  3. setfill
  4. internal
  5. setprecision
  6. showpoint
  7. fixed
  8. scientific

endl – endline manipulator

The endl manipulator, when used in an output statement, causes a line feed to be insert. Just like a “\n” new line feed character.

Example: Suppose values of m=20, n=25.75 and p=125 then,


setw – setwidth manipulator

setw manipulator allows a specified width for a field that is to be printed on screen and by default the value printed is right justified.

The field width is the number of characters that can be written to a field. If the output string is larger than the field width, the output is not truncated but the field is extended.

The output will always contain at least the number of digits specified as the field width. You can either use the width() method or the setw() manipulator to define field width.


cout << setw(6);

The default field width is 0. You can also use the width() method to get the current field width. To do so, call width() without any other arguments.

int fieldwidth = cout.width();

One special attribute of the field width is the fact that this value is non-permanent. The field width specified applies to the next output only, as is illustrated by the examples on the opposite page. The first example outputs the character ‘X’ to a field with width of 6, but does not output the ‘|’ character.

Example 1:

cout << '|' << setw(10) << 'X' << '|';
|     X|              // Field width 10 with right justified

Example 2:

cout<<setw(10)<<123.4<< endl 
<<"1234567890 << endl;
    123.40              // Field width 10

setfill -Fill Characters and Alignment

If a field is larger than the string you need to output, blanks are used by default to fill the field. You can either use the fill() method or the setfill() manipulator to specify another fill character.


cout << setfill('*') << setw(5) << 12;
Output: ***12

The fill character applies until another character is defined. As the previous example shows, output to fields is normally right-aligned.

The other options available are left-aligned and internal, which can be set by using the manipulators left and internal.

The internal manipulator left-justifies the sign and right justifies the number within a field.


cout <<internal<<-123; 
Output: -00123

setprecision– set floating point precision

The standard settings can be modified in several ways. You can
■ change the precision, i.e. the number of digits to be output
■ force output of the decimal point and trailing zeroes
■ stipulate the display mode (fixed point or exponential).
Both the manipulator setprecision( ) and the method precision() can be used to redefine precision to be used. Floating-points are displayed to six digits by default.

Decimals are separated from the integral part of the number by a decimal point. Trailing zeroes behind the decimal point are not printed. If there are no digits after the decimal point, the decimal point is not printed (by default).


cout << 1.0;        // Output: 1
cout << 1.234;      // Output: 1.234
cout << 1.234567;   // Output: 1.23457

The last statement shows that the seventh digit is not simply truncated but rounded. Very large and very small numbers are displayed in exponential notation.

cout << 1234567.8;

Both the manipulator setprecision() and the method precision() can be used to redefine precision to be used.

cout << setprecision(3); // Precision: 3
// or: 
cout << 12.34; 
// Output: 12.340

In contrast, you can use the scientific manipulator to specify that floating-point numbers are output as exponential expressions.


cout << left // Left-aligned
<< setfill('?') // Fill character ?
<< setw(20) << "spring flowers" ; // Field width 20
"spring flowers??????"

here, left is used to set the data to left justified and right is to set data right justified format.


The manipulator showpoint outputs the decimal point and trailing zeroes. The number of digits being output (e.g. 6) equals the current precision.The number of digits after the decimal point corresponds to the used precision.

cout << showpoint << 1.0; // Output: 1.00000


Output in fixed point notation. fixed point output with a predetermined number of decimal places is often more useful. In this case, you can use the fixed manipulator with the precision defining the number of decimal places. The default value of 6 is assumed in the following example.

cout << fixed << 66.0; 
// Output: 


Output in scientific point can use the scientific manipulator to specify that floating-point numbers are output as exponential expressions.

double x1 = -123.456;
cout << scientific << setprecision(4) << x1 << endl;

Manipulators are special functions that can be included in program statements to alter the format parameters of a stream in IO console.

Related posts