Updated on Oct 3, 2023 10:26 IST

This article explains the concept of setw in C++. The setw function is part of the iomanip library and allows for adjusting the field width for output operations. The setw manipulator sets the width of the output field to the specified value. The article includes code examples to show how setw can be used to output tables with consistent widths and mathematical expressions with consistent widths and precisions, making the output easier to read and compare. The article also covers the syntax for using the setw function.

2023_02_MicrosoftTeams-image-6.jpg

When working with multiple values that need to be displayed, they must have a consistent width. The setw() function in C++ helps ensure that the width of the values is consistent, making it easier to read and compare the values. This tutorial will discuss setw in C++ and understand its working through code examples. 

We will be covering the following sections:

What is Setw in C++?

Setw in C++ is a function from the iomanip library that adjusts the field width for output operations. The function, set width, specifies the minimum number of character positions a variable will occupy. Setw helps to define the width of the field used for output. It takes in an argument for the new width value and applies it to the next output operation performed on the specified stream. The setw manipulator sets the width of the output field to the specified value.

 Syntax

The syntax to declare setw in C++ is shown below:


 
setw(width)
Copy code

Where width is an integer that specifies the desired field width for the output data type of the first element.

Stored procedure Vs. Function: What are the differences?
Understanding Logical Operators in C
All About OOPs Concepts in C++

Preparing for a C++ interview? Check out the Top C++ Interview Questions and Answers

Also Read – Understanding Operators in C++

Learn more: Basics of C Programming Language

Related Read – Binary Search in C++

Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
4.24 K
6 weeks
– / –
15 days
– / –
– / –
7.14 K
5 weeks
name
SoloLearnCertificateStar Icon4.5
Free
– / –
– / –
4 months
– / –
3 months

How Setw Works in C++?

The setw() function in C++ sets the width of the field to the number specified within the parenthesis. 

Here is an example of how you could use setw to output the values of two variables, x, and y, with a field width of 10:


 
#include <iomanip>
#include <iostream>
int main() {
int x = 42;
double y = 3.14;
std::cout << std::setw(10) << x << std::setw(10) << y << std::endl;
return 0;
}
Copy code

Output:

42 3.14

Explanation:

The code declares two variables, x and y, with values 42 and 3.14, respectively.

In the main function, the code uses the std::setw manipulator to set the field width to 10 characters for the output of both x and y. The values of x and y are then output to the standard output stream std::cout.

The std::endl manipulator prints a newline character after the values of x and y have been output.

Examples of Using Setw in C++

Example 1:

Suppose you have a program that outputs a table of values with multiple columns. You want the columns to be aligned and have a consistent width so that the table is easier to read. You can use the setw manipulator to specify the width of each column and ensure that the values are properly aligned.

Here is an example of how you could use setw to output a table of product information:


 
#include <iomanip>
#include <iostream>
#include <string>
int main() {
std::string product_names[] = { "Keyboard", "Mouse", "Monitor", "Speakers" };
int product_prices[] = { 30, 20, 150, 60 };
std::cout << std::left << std::setw(20) << "Product Name"
<< std::right << std::setw(10) << "Price" << std::endl;
for (int i = 0; i < 4; i++) {
std::cout << std::left << std::setw(20) << product_names[i]
<< std::right << std::setw(10) << product_prices[i] << std::endl;
}
return 0;
}
Copy code

Output:

Product Name Price

Keyboard 30

Mouse 20

Monitor 150

Speakers 60

In the above program, the std::left and std::right manipulators are used to specify the alignment of the values within the columns. The std::left manipulator aligns the values to the left, and the std::right manipulator aligns the values to the right. The std::setw manipulator is used to set the width of each column. The first column’s width is set to 20 characters, and the width of the second column is set to 10 characters.

In this example, setw helps ensure that the output of the table is neat and organized, making it easier to read.

Example 2:

Let’s take one more example. Suppose you have a program that calculates and outputs the result of mathematical expressions. You want the results to be displayed with a consistent width and precision so that they are easier to read and compare. You can use the setw and setprecision manipulators to specify the width and precision of the results.

Here is the code of how you could use setw and setprecision to output the results of mathematical expressions:


 
#include <iomanip>
#include <iostream>
#include <cmath>
int main() {
double x = sqrt(2);
double y = log10(100);
std::cout << std::setw(10) << std::left << "sqrt(2)"
<< std::setw(10) << std::right << std::setprecision(3) << x << std::endl;
std::cout << std::setw(10) << std::left << "log10(100)"
<< std::setw(10) << std::right << std::setprecision(3) << y << std::endl;
return 0;
}
Copy code

Output:

sqrt(2) 1.41

log10(100) 2

In the above program, the std::setw manipulator is used to set the width of the output. The std::left and std::right manipulators are used to specify the alignment of the values within the columns. The std::setprecision manipulator is used to set the precision of the output. The values of x and y are calculated using the sqrt and log10 functions from the cmath library.

In this example, setw and setprecision help ensure that the output of the results is neat, organized, and easily comparable, making it easier to read.

What is Override Keyword in C++?
Understanding C++ Namespaces and its Methods
Power Function in C

Endnotes

C++ setw is a useful tool for formatting output in a neat and organized manner, which can improve the readability and overall appearance of the output. I hope this article helped you grasp the concept of setw in C++. 

Explore our C++ articles to learn more about the language and consolidate your knowledge of the fundamentals. 

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio