How to use the setw Manipulator in C++? Last Updated : 17 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the std::setw manipulator, which stands for “set width”, is a function provided by the <iomanip> library. The width of the field in which the output of the subsequent value will be shown can be adjusted using the setw() manipulator. In this article, we will learn how we can use the setw manipulator in C++. Example: Input: num = 50; Output: 50 //field width set to 10C++ setw ManipulatorIn C++, the setw() manipulator is mainly used to set the width of the next input-output operation according to the number passed to this function as argument. It ensures formatting consistency by padding the output with spaces to fit the set width. Syntax of setwsetw(int n);Here, n is an integer value upto which the field width is to be set. C++ Program to Show How to Use setw ManipulatorThe below example demonstrates the use of setw() function for setting the width for output value in C++. C++ // C++ program to show how to use setw() manipulator #include <iomanip> #include <iostream> using namespace std; int main() { // Initialize an integer variable. int num = 50; // Print the integer before setting the width. cout << "Before setting the width:" << endl << num << endl; // Set fill character to '-'. cout << setfill('-'); // Setting the width using setw to 10 and printing the // integer. cout << "Setting the width using setw to 10:" << endl << setw(10); cout << num; return 0; } OutputBefore setting the width: 50 Setting the width using setw to 10: --------50Time Complexity: O(1)Auxilliary Space: O(1) Explanation: In the above example the setw() is used to increase the width of the output and the setfill() manipulator fills the empty character with the given char('-') argument. Comment More infoAdvertise with us Next Article How to Right Justify Output in C++? S sushmaa1ii Follow Improve Article Tags : C++ Programs C++ cpp-input-output cpp-manipulators CPP Examples +1 More Practice Tags : CPP Similar Reads How to Use Bit Manipulation Methods in C++ Bit manipulation is a technique used for optimizing performance and memory usage in various programming scenarios. It is very useful from a Competitive Programming point of view. In this article, we will learn how we can use bit manipulation methods in C++ and provide examples to help you understand 5 min read How to Use the std::mutex Synchronization Primitive in C++ In multi-threaded programming, it is essential to ensure that shared resources are accessed in a controlled and synchronized manner to maintain data consistency and prevent race conditions. The std::mutex synchronization primitive was introduced in C++ 11 to allow threads to acquire exclusive owners 3 min read How to Write Getter and Setter Methods in C++? In C++, classes provide a way to define custom data types that can encapsulate data and methods related to that data. Getter and setter methods are commonly used to access and modify private member variables of the class allowing for the controlled access and modification of data. In this article, w 2 min read How to Right Justify Output in C++? In C++, the outputs produced by the program are by default to the left of the screen. In this article, we will learn how to right justify the output in C++. Example: Input:float x= 123.45float y= 6.7899Output: 123.45 6.7899 // justified on the consoleâs right side.Right Justifying the Output in C++T 2 min read Digital Clock starting from user set time in C++ In this article, we will discuss the Digital Clock in C++ Language. It is an application that allows for a personal clock that starts at a custom set time and shows the time from that point onwards. This article describes how to make such a clock in a 24-hour format with HH:MM:SS slots and start the 3 min read C++ Program to Implement Half Subtractor A Half Subtractor is a digital logic circuit that is used to subtract two single-bit binary numbers. In this article, we will learn how to implement the half subtractor logic in C++.What is a Half Subtractor?As told earlier, half subtractor is a digital logic circuit that makes the use of logical ga 2 min read Like