C++ Manipulators endl, setw, setfill, setprecision with Examples

C++ Manipulators:

C++ manipulators are used to changing the format of the output. C++ Manipulators are instructions to the I/O stream object that modify the I/O format in various ways, the endl, setw, setfill, and setprecision are examples f C++ Manipulators of c++, mostly these C++ Manipulators are used with ‘cout’ object as independent parameters. Most of the C++ Manipulators are defined in “iomanip.h” header file except ‘endl’.

‘endl’ C++ Manipulators:

The word ‘endl’ stands for end of line. The endl C++ Manipulators are used to move the cursor t the beginning of the next line. It works similar to the ‘\n’ escape sequence.

The following statement displays a message in three lines using endl C++ Manipulators:


the output of the above statement will be:




The above statement is equivalent to the following statement:


both the above output statement have the same output. The difference between ‘endl’ and ‘\n’ is that each ‘endl’ manipulator use an independent insertion operator without quotation marks. So four insertion operators (<<) are used with the statement. In the second statement ‘\n’ is inserted into the string and only one insertion operator (<<) is used with the statement.

Now consider the following statement:
cout<<”C++ is a powerful language”<<endl;

This statement is equivalent to:

cout<<”C++ is a powerful language \n”;

the ‘setw’ C++ Manipulators:

the word ‘setw’ stands for set width. The setw C++ Manipulator is used to set the field width of the output on the output device. By default, the output is displayed/printed right-justified within the specified field of setw C++ Manipulators.

The general syntax of setw manipulator is as follows:


the ‘n’ indicates the field width (i.e. number of columns). It is a integer value. it the specified field width is less than the width of the output data that is to be displayed, then there is no effect of setw manipulator. The output will be displayed or printed in normal way.

The setw C++ Manipulator is the part of “iomanip.h” header file. This header file must be included in the program to use setw manipulator.

Example following program displays different string and numeric values in the specified field with using setw C++ Manipulator:

C++ Manipulators

In the above program, when the first statement will be executed, the word “English” will be printed on the screen right justified in first 10 columns and then “9” will be printed right justified in columns 11-15, similarly, the output of other statements will be printed in the same way.

We can also use setw manipulator with cin stream input object to accept the specified number of characters into the input buffer. For example;

char name[15];


In the above statement the variable “name” of string type (or array of characters) is declared and setw is used with input stream “cin”. Only 10 characters (including null character) will be accepted in input buffer.

Example Write a program that used setw manipulator to display output on the computer screen:

C++ Manipulators

The setfill C++ Manipulators:

The setfill C++ Manipulators is used to fill the leading blank spaces in the output with a specified character. This C++ Manipulator is used along with setw C++ Manipulator.

The general syntax of setfill manipulator is as follows:


Where “character” indicates the character that is to be filled in places of leading blank spaces. It may be a character constant or character variable or an ASCII value for a character.

For example:


In the above statement setw manipulator will set the field width of 15 columns. The string “Programming” will take only 11 columns on the display screen. So the leading blank spaces will be filled with ‘*’ character (as specified in setfill character).

Example write a program that displays different strings using setfill C++ Manipulator:

C++ Manipulators

The setprecision C++ Manipulators:

The setprecision C++ Manipulators are used to set the number of digits to be displayed after a decimal point. The output value is rounded with the use of this manipulator. For example, to display a value 49.917 with two digits of precision, the value 49.92 will be displayed on the output.

The general syntax f setprecision manipulator is as follows:



n: it indicates the number of digits to be displayed after the decimal point. It can be an unsigned positive integer constant, variable or expression.

Example write a program that displays a floating-point number using setprecision C++ Manipulator:

C++ Manipulators

Related Article:


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Back to top button