IOMANIP Functions: C++ Setprecision & C++ Setw With Examples

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 10, 2024

This Tutorial Describes a few IOMANIP header Functions to Manipulate the Output of C++ Programs like setprecision and setw.

The header <iomanip> consists of functions that are used to manipulate the output of the C++ program. We can make the output of any program neater and presentable based on where we want to show it or who is going to use it.

=> Check Out The Perfect C++ Training Guide Here.

IOMANIP Functions

IOMANIP Functions In C++

To format the output properly, we can use the manipulators provided by the <iomanip> header and make the output presentable.

For Example, if we are printing say a matrix as follows:

iomanip example

Using a simple cout stream we may not be able to format the output as shown above. Hence we can use the setw function from <iomanip> header, and we can set the specific width between the elements.

This way we can make the program output to look more realistic and presentable.

<iomanip> header contains several functions to format the output.

The main ones among them include:

  • Setprecision: This function sets the precision for decimal or float values.
  • setw: Setw function sets the field width or number of characters that are to be displayed before a particular field.
  • Setfill: Setfill function is used to fill the stream with char type c specified as a parameter.

C++ SetPrecision

Function Prototype: setprecision (int n).
Parameter(s): n=>value of the decimal precision to be set.
Return Value: unspecified
Description: This function sets the decimal precision for floating-point values. This formats the floating-point when displayed.

Example:

Given below is a detailed C++ example to demonstrate the setprecision function.

#include <iostream>     
#include <iomanip>      
using namespace std;
int main () 
{
  double float_value =3.14159;
  cout << setprecision(4) << float_value << '\n';
  cout << setprecision(9) << float_value << '\n';
  cout << fixed;
  cout << setprecision(5) << float_value << '\n';
  cout << setprecision(10) << float_value << '\n';
  return 0;
}

Output:

SetPrecision Output

Here we are setting various precisions for the float value 3.14159. As we can see from the output, the display of float value changes depending on the precision set.

Setw In C++

Function Prototype: setw (int n).
Parameter(s): n=> value of the field width (number of characters) to be used.
Return Value: unspecified
Description: Function setw sets the field width or the number of characters to be used for outputting numbers.

Example:

The setw function is demonstrated using a C++ program.

#include <iostream>     
#include <iomanip>      
using namespace std;
int main () {
  cout << "The number printed with width 10"<<endl;
  cout << setw(10);
  cout << 77 << endl;
  
  cout << "The number printed with width 2"<<endl;
  cout << setw(2);
  cout << 10 << endl;
  
  cout << "The number printed with width 5"<<endl;
  cout << setw(5);
  cout << 25 << endl;
  return 0;
}

Output:

Setw Output

In this program, we print different numbers by setting different values of width. As per the width set, the number is printed after skipping those many spaces. The output of the program shows the difference clearly.

C++ Setfill

Function Prototype: setfill (char_type c).
Parameter(s): n=> new fill character for the stream; char_type: type of characters used by stream.
Return Value: unspecified
Description: setfill sets c as the new fill character for the stream.

Example:

Given below is an example C++ program to demonstrate setfill.

#include <iostream>     
#include <iomanip>      
using namespace std;
int main () {
  cout << setfill ('*') << setw (10);
  cout << 15 << endl;
  cout << setfill ('#') << setw (5);
  cout << 5 << endl;
  cout << setfill ('#') << setw (5);
  cout << 1 << endl;
  cout << setfill ('*') << setw (10);
  cout << 25 << endl;
  return 0;
}

Output:

Setfill Output

In the above program, we have used setfill function along with various characters as the setfill function parameters. When we call this function with setw function, the width we have specified in the setw function is filled by the character we specified in the setfill function.

Conclusion

The header <iomanip> contains the functions that we can use to format the output of the C++ program. These functions can be used one at a time or together to make the output of our program more presentable.

In this tutorial, we have seen the functions setprecision, setw and setfill of <iomanip> header and also developed C++ programs using them. These functions can be very useful when we need to format and beautify the output.

In our next tutorial, we will discuss various functions from the <math> header.

=> Check Here To See A-Z Of C++ Training Tutorials Here.

Was this helpful?

Thanks for your feedback!

Leave a Comment