setw() function in C++ with Examples Last Updated : 20 Feb, 2025 Comments Improve Suggest changes Like Article Like Report The setw() method of iomanip library in C++ is used to set the ios library field width based on the width specified as the parameter to this method. The setw() stands for set width and it works for both the input and the output streams.Syntaxsetw(int n);Parameters:n: It is the integer argument corresponding to which the field width is to be set.Return Value:This method does not return anything. It only acts as a stream manipulator.ExamplesThe following examples demonstrate the use setw() function in C++ programs:Setting Integer Print Width C++ #include <iomanip> #include <ios> #include <iostream> using namespace std; int main() { int num = 50; cout << "Before setting the width: \n" << num << endl; // Using setw() cout << "Setting the width" << " using setw to 5: \n" << setw(5); cout << num; return 0; } OutputBefore setting the width: 50 Setting the width using setw to 5: 50In the above example, we have used setw() to add padding to the integer output. We can use setw() for many such cases. Some of those are stated below.Limiting Input Characters using setw() C++ #include <iostream> #include <iomanip> using namespace std; int main() { string str; // setting string limit to 5 characters cin >> setw(5) >> str; cout << str; return 0; } InputGeeksforGeeksOutputGeeksLimiting Output String Characters using setw() C++ #include <iostream> #include <iomanip> using namespace std; int main() { string str("GeeksforGeeks"); // adding padding cout << "Increasing Width:\n" << setw(20) << str << endl; // reducing width cout << "Decreasing Width:\n" << setw(5) << str; return 0; } OutputIncreasing Width: GeeksforGeeks Decreasing Width: GeeksforGeeksAs the above example illustrate, we can increase the width of the string output using setw() but cannot decrease the output width of the string to less than the actual characters present in it. Comment More infoAdvertise with us Next Article setw() function in C++ with Examples G guptayashgupta53 Follow Improve Article Tags : C++ cpp-input-output CPP-Functions cpp-manipulators Practice Tags : CPP Similar Reads fill() function in C++ STL with examples The fill() function in C++ STL is used to fill some default value in a container. The fill() function can also be used to fill values in a range in the container. It accepts two iterators begin and end and fills a value in the container starting from position pointed by begin and just before the pos 2 min read ios operator() function in C++ with Examples The operator() method of ios class in C++ is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: operator void*() const; Parameters: This method does not accept any parameter. Return Value: This method returns a null pointer if any error bit is set of this 1 min read ios setstate() function in C++ with Examples The setstate() method of ios class in C++ is used to change the current state of this stream by setting the flags passed as the parameters. Hence this function changes the internal state of this stream. Syntax: void setstate(iostate state) Parameters: This method accepts the iostate as parameter whi 2 min read Methods vs. Functions in C++ with Examples A method is a procedure or function in OOPs Concepts. Whereas, a function is a group of reusable code which can be used anywhere in the program. This helps the need for writing the same code again and again. It helps programmers in writing modular codes. Methods: A method also works the same as that 3 min read ios clear() function in C++ with Examples The clear() method of ios class in C++ is used to change the current state of the specified flag by setting it. Hence this function changes the internal state of this stream. Syntax: void clear(iostate state) Parameters: This method accepts the iostate as parameter which is the flag bit to be set in 2 min read Like