C++ setw() Function

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

In C++, the setw function is a manipulator for set width. The setw method is found in the iomaip library included in C++.

Using the setw function (manipulator), you can specify the width of a field in the iOS library or the minimum number of characters a variable will consume. In simple terms, you can set the width of a field for output operations using the setw C++ function.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of C++ setw()

Parameters of C++ setw()

C++ setw() takes only one parameter, which is an integer. The field width is determined by the number of characters specified in the argument.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Return Value of C++ setw()

C++ setw() returns nothing and is only used as a stream manipulator.

Exceptions of C++ setw()

Basic guarantee − if an exception is thrown, the stream is valid.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Examples

When the Set Width Is Greater than The Length of The Output

Code:

Output

Explanation:

The code mentioned above explains how the setw function operates. The iomanip library has been imported. The value for the setw()  function has then been set to 6 after that. It starts over at zero. Then, as an output, we send a number. You can see the difference in spacing by looking at the result, which is displayed.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

When the Set Width Is Equal to The Length of The Output

Code:

Output

Explanation:

The code mentioned above explains how the setw function operates when the set width is equal to the length of the output. The iomanip library has been imported. The value for the setw() function has then been set to 7 after that. It starts over at zero. Then, as an output, we send the number 5676891. Since the set width is equal to the length of the output, the output covers all the widths mentioned.

When the Set Width Is Less than The Length of The Output

Code:

Output

Explanation:

The code mentioned above explains how the setw function operates when the set width is less than the length of the output. The iomanip library has been imported. The value for the setw()  function has then been set to 3, less than the length of output 456746.

The size of the number exceeds the size of the output. The setw C++ function's unique feature is that it leaves the number unchanged even if the field's width is less. Instead, it displays the complete number. Therefore, the complete number is returned if the argument supplied is less than the size of the number.

How Does C++ setw() Work?

The setw() function is used to set the width of a field using the number provided as arguments. We can better understand this by looking at the code below.

Code:

Output

Explanation:

We can determine from the code lines that we used standard libraries. There is one with an input-output stream and another with an input-output manipulation library, to which setw() belongs.  Without importing these libraries, the function will not function.

The width of the output field is then set by the main function using the setw() function. We output the string as "abc" after setting the setw() function to 8. This field will initially be set to 0, and when the appropriate number is entered, it will be changed to 8 in this case. The following is how it will operate.

As the string is three characters long, the first five spaces will be empty (the above output contains underscores to represent the white spaces). The string we provided as output is used to write the last three positions.

Conclusion

  • The setw() function is used to set the width of a field taken as an argument.
  • The setw() function takes only one parameter, which is an integer.
  • The given argument to the setw() function can be greater than, equal to, or less than the length of the output.
  • In each case, the complete data is returned even if the argument supplied is less than the size of the number.
  • When the set width exceeds the output's length, the setw() function will leave the first required spaces empty.