Javatpoint Logo
Javatpoint Logo

C++ Manipulator setw

C++ manipulator setw function stands for set width. This manipulator is used to specify the minimum number of character positions on the output field a variable will consume.

This manipulator is declared in header file <iomanip>.

Syntax

Parameter

n: number of characters to be used as filed width.

Return value

This function returns an object of unspecified type. The setw function should only be used as a stream manipulator.

Data Races

The stream object on which it is inserted or extracted is modified and concurrent access to the same stream object may introduce data races.

Exceptions

Object is in a valid state, if any exception is thrown.

Example 1

Let's see the simple example to demonstrate the use of setw:

Output:

24

Example 2

Let's see another simple example:

Output:

no setw:42
setw(6):    42
setw(6), several elements: 89    1234
Input from "hello, world" with setw(6) gave "hello"

Example 3

Let's see another simple example:

Output:

  200  300
   200   300
    200    300
     200     300
Next TopicC++ Manipulator





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA