Setw Field Width C++

C++ includes many functions that are divided into many categories. One of the functions that are discussed here is known as “setw” C++. This guide will contain some basic examples of the “setw” function performed in C++.

“Setw” is an example of manipulators that are used in C++ programs by modifying the input and output stream. These manipulators are of two types. These functionalities of manipulators are defined in the header file. “Setw” is an example of manipulators with arguments.

We will explain this concept on the Linux operating system, so you need to have Ubuntu installed and in the running form on your system. So you must install Virtual Box and, after downloading and installing, configure it. Now add the Ubuntu file to it. You can access Ubuntu’s official website and download the file according to your system requirement and operating system. It will take hours, then after installation, configure it on the virtual machine. In the configuration process, make sure you have created the user because it is essential for any operation on the Ubuntu terminal. Moreover, Ubuntu needs the authentication of the user before doing any installation.

We have used the 20.04 version of Ubuntu; you may use the latest one. For the implementation, you need to have a text editor and access the Linux terminal because we will be able to see the output of the source codes on the terminal through the query.

Setw Function

“Setw” is a method that is present in the “iomaip” library. It is a manipulator of C++ that means ‘set width’. This manipulator is used to set the field width or display the space that how much the leftmost character is lifted a specific distance from the terminal border. In other words, the “setw” C++ is used to set the field width for the output purpose. The width is given as arguments and the statement or a string on which this function is to be applied. The parameter of width is also set by the function that includes stream in and streams out.

Whenever this function is called, it takes the number width as an argument


# Setw(int number)

Number portion inside the parenthesis contains the number of characters to set as the width for that character.  The output is designed by using out<<setw(num), and the input can be get by using in>>setw(num). A function “setw” is built so that it can take only a single parameter that is an integer. This function is not capable of returning anything.

Example 1

As we have described earlier, that “setw” feature is explained in the library ‘iomanip’. So it is an additional library other than “iostream” that is used to transform the code.

# include <iomanip>

Then we used the main function here that contained direct streaming of the numbers. First, we create the distance of 0 points from the left-most margin on the terminal screen, and that is done by:

# std:: setw(10);

When a space is created, then we simply display the value of whatever we want. This function returns no value.  After adding the code in the text editor in Ubuntu operating system, you need to save this file with the extension of C/C++. The name of the file is set as “setw.c”.

Now, we want to see the resultant value of the code. We have used a Linux terminal to display the results of the files. We always need to have a compiler for the compilation and execution of code. Hence we need G++. Because for the programs of C++ to get executed. ‘-o’ is used to save the output of source code in a file.

$ g++ -o setw setw.c

$ setw

By executing the code, you can see that the value we entered is displayed at a distance like it is displayed normally at the left side of the terminal.

Example 2

In this example, we have used three libraries to interact with the user as well. Like

# include <ios>

The other two libraries are the same as used before. Now going to the main program, first initializes a variable with a number of your own choice. This example relates to the difference between using the setw() function and without using this function. First, directly the number is displayed without applying the width function on it. After that, we will use the width setting function with the length we want to add in width. Then after the settlement of width, the number is displayed. Here the method of width setting is changed from the previous example. so we can also use this method directly in the ‘cout’ statement while displaying the number

# Cout<< setw(10);

Compile the code and execute it on the terminal. From the result, you can see that it is discriminated between the two values. The distance can easily be determined.

Example 3

While creating a major difference in each line, we somehow created stairs like output. First, after the libraries declaration, in the main program, two variables are declared. In these two variables, we initialize the values. Now, these values are displayed with a different distance. In each line, both the values are displayed with the same width distance, but in each line, the number is not constant anymore; it is incremented by 1 always. Now it’s upon you how many times you want to display the values respectively.

# Cout << setw (5) << a << setw (5) << b<< endl;

Now compile the code and execute it. Upon execution, you will see that each line contains an equal distance between the two numbers escaped with equal width. But in each line, this distance is increased from the terminal left border.

Example 4

This example also contains the same concept by using the same header at the start of the code. In the main program, “setw” is again used in the ‘cout’ statement. Two lines with the same width distance can be used. But here, we have displayed the number with a separated distance. First, we have set the width size with the use of std.

# Std :: cout << std :: setw(10);

All other values we want to print can be used in lines by using this. After changing the width to 10, are reset again by displaying the number. Once we have passed a number, and in the next line, we have introduced a string displayed separately. Before displaying the string, we have increased the width size.

Now compile the code and then execute it on the Linux terminal. You can see the resultant value easily.


‘Setw field width C++’ is an article that contains all the aspects of its usage in the C++ program that is implemented on the text editor in the Ubuntu Linux system and compiled and executed on the Linux terminal. This argument-based manipulator takes the number and applies that on distancing the compiler from the side of the terminal screen. In other words, the width is increased. Each example is explained to escalate your knowledge regarding this manipulator.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.