C++ Programming

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

C++ Manipulators:

C++ manipulators are used to change 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++ Manipulator 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’.

Amazon Purchase Links:

Top Gaming Computers

allpcb circuit

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

‘endl’ C++ Manipulators:

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

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

cout<<”object”<<endl<<”Oriented”<<endl<<”Programming”;

the output of the above statement will be:

object

Oriented

Programming


The above statement is equivalent to the following statement:

cout<<”object\nOriented\nProgramming”;

both the above output statement has the same output. The difference between ‘endl’ and ‘\n’ is that each ‘endl’ manipulator uses 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++ Manipulators are 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++ Manipulator.

The general syntax of setw manipulator is as follows:

setw(n)

the ‘n’ indicates the field width (i.e. number of columns). It is an 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 a normal way.

The setw C++ Manipulators 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++ Manipulators:

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 the 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];

Cin>>>setw(10)>>name;

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 the 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 are used to fill the leading blank spaces in the output with a specified character. This C++ Manipulator is used along with setw C++ Manipulators.

The general syntax of setfill manipulator is as follows:

Setfill(character)

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:

Cout<<setw(15)<<setfill(‘*’)<<”electronic”;

In the above statement, setw manipulator will set the field width of 15 columns. The string “electronic” 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++ Manipulators:

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:

Setprecisin(n)

Where

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++ Manipulators:

C++ Manipulators

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

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

Back to top button