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 info G guptayashgupta53 Follow Improve Article Tags : C++ cpp-input-output CPP-Functions cpp-manipulators Explore C++ BasicsIntroduction to C++3 min readData Types in C++7 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++10 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library2 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like