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 iomanip setfill() function in C++ with Examples The setfill() method of iomanip library in C++ is used to set the ios library fill character based on the character specified as the parameter to this method. Syntax: setfill(char c) Parameters: This method accepts c as a parameter which is the character argument corresponding to which the fill is t 2 min read ios operator() function in C++11 with Examples The operator() method of ios class in C++11 is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: explicit operator bool() const; Parameters: This method does not accept any parameter. Return Value: This method returns false if any error bit is set of this 1 min read sin() function for complex number in C++ with Examples The sin() function for complex numbers is defined in the <complex> header file. This function is the complex version of the sin() function. This function is used to calculate the complex sine of the complex number z. This function returns the sine of the complex number z. Syntaxsin (z); Parame 2 min read Array of list in C++ with Examples What is an array? An array in any programming language is a data structure that is used to store elements or data items of similar data types at contiguous memory locations and elements can be accessed randomly using indices of an array. Arrays are efficient when we want to store a large number of e 5 min read set find() Function in C++ STL The std::set::find() is a built-in function in C++ STL that is used to find an element in the set container. It is a member function of std::set container so we can use it directly with any set object.Syntax set_name.find(key) Parameterskey: The element which we have to find.Return ValueIf the eleme 2 min read Like