changing field widththe printf and scanf


Changing Field width

The printf() and scanf() functions from the standard library, use width specifier for controlling the width of its output . Similarly, the ostream class also has a member function width() for the same. This function has the following prototype.

                int width(void);

                int width(int newsize);

The first form returns the current field width. The default is always zero. That is because, the cout object always uses space just wide enough to output the values and a default size if zero matches all. The second form can be used to alter the current width setting. The argument passed to it is the new width to be set. It also returns the previous width. The width () function affects only the next item displayed. The filed width reverts to default afterwards.

e.g.

                void main()

                 {

                                cout<<" default width="<< cout.width()<

                                cout<<"[";

                                cout.width(10);                                 Set width to 10

                                cout<<"A";

                                cout<<"]";

                }

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: changing field widththe printf and scanf
Reference No:- TGS0309500

Expected delivery within 24 Hours