Formatted I/O in C++ Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report C++ helps you to format the I/O operations like determining the number of digits to be displayed after the decimal point, specifying number base etc. Example: If we want to add + sign as the prefix of out output, we can use the formatting to do so: stream.setf(ios::showpos) If input=100, output will be +100 If we want to add trailing zeros in out output to be shown when needed using the formatting: stream.setf(ios::showpoint) If input=100.0, output will be 100.000 Note: Here, stream is referred to the streams defined in c++ like cin, cout, cerr, clog. There are two ways to do so: Using the ios class or various ios member functions. Using manipulators(special functions) Formatting using the ios members: The stream has the format flags that control the way of formatting it means Using this setf function, we can set the flags, which allow us to display a value in a particular format. The ios class declares a bitmask enumeration called fmtflags in which the values(showbase, showpoint, oct, hex etc) are defined. These values are used to set or clear the format flags. Few standard ios class functions are: width(): The width method is used to set the required field width. The output will be displayed in the given width precision(): The precision method is used to set the number of the decimal point to a float value fill(): The fill method is used to set a character to fill in the blank space of a field setf(): The setf method is used to set various flags for formatting output unsetf(): The unsetf method is used To remove the flag setting Working: CPP #include<bits/stdc++.h> using namespace std; // The width() function defines width // of the next value to be displayed // in the output at the console. void IOS_width() { cout << "--------------------------\n"; cout << "Implementing ios::width\n\n"; char c = 'A'; // Adjusting width will be 5. cout.width(5); cout << c <<"\n"; int temp = 10; // Width of the next value to be // displayed in the output will // not be adjusted to 5 columns. cout<<temp; cout << "\n--------------------------\n"; } void IOS_precision() { cout << "\n--------------------------\n"; cout << "Implementing ios::precision\n\n"; cout << "Implementing ios::width"; cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout<<3.1422; cout << "\n--------------------------\n"; } // The fill() function fills the unused // white spaces in a value (to be printed // at the console), with a character of choice. void IOS_fill() { cout << "\n--------------------------\n"; cout << "Implementing ios::fill\n\n"; char ch = 'a'; // Calling the fill function to fill // the white spaces in a value with a // character our of choice. cout.fill('*'); cout.width(10); cout<<ch <<"\n"; int i = 1; // Once you call the fill() function, // you don't have to call it again to // fill the white space in a value with // the same character. cout.width(5); cout<<i; cout << "\n--------------------------\n"; } void IOS_setf() { cout << "\n--------------------------\n"; cout << "Implementing ios::setf\n\n"; int val1=100,val2=200; cout.setf(ios::showpos); cout<<val1<<" "<<val2; cout << "\n--------------------------\n"; } void IOS_unsetf() { cout << "\n--------------------------\n"; cout << "Implementing ios::unsetf\n\n"; cout.setf(ios::showpos|ios::showpoint); // Clear the showflag flag without // affecting the showpoint flag cout.unsetf(ios::showpos); cout<<200.0; cout << "\n--------------------------\n"; } // Driver Method int main() { IOS_width(); IOS_precision; IOS_fill(); IOS_setf(); IOS_unsetf(); return 0; } Output: -------------------------- Implementing ios::width A 10 -------------------------- -------------------------- Implementing ios::fill *********a ****1 -------------------------- -------------------------- Implementing ios::setf +100 +200 -------------------------- -------------------------- Implementing ios::unsetf 200.000 -------------------------- Formatting using Manipulators The second way you can alter the format parameters of a stream is through the use of special functions called manipulators that can be included in an I/O expression. The standard manipulators are shown below: boolalpha: The boolalpha manipulator of stream manipulators in C++ is used to turn on bool alpha flag dec: The dec manipulator of stream manipulators in C++ is used to turn on the dec flag endl: The endl manipulator of stream manipulators in C++ is used to Output a newline character. and: The and manipulator of stream manipulators in C++ is used to Flush the stream ends: The ends manipulator of stream manipulators in C++ is used to Output a null fixed: The fixed manipulator of stream manipulators in C++ is used to Turns on the fixed flag flush: The flush manipulator of stream manipulators in C++ is used to Flush a stream hex: The hex manipulator of stream manipulators in C++ is used to Turns on hex flag internal: The internal manipulator of stream manipulators in C++ is used to Turns on internal flag left: The left manipulator of stream manipulators in C++ is used to Turns on the left flag noboolalpha: The noboolalpha manipulator of stream manipulators in C++ is used to Turns off bool alpha flag noshowbase: The noshowbase manipulator of stream manipulators in C++ is used to Turns off showcase flag noshowpoint: The noshowpoint manipulator of stream manipulators in C++ is used to Turns off show point flag noshowpos: The noshowpos manipulator of stream manipulators in C++ is used to Turns off showpos flag noskipws: The noskipws manipulator of stream manipulators in C++ is used to Turns off skipws flag nounitbuf: The nounitbuf manipulator of stream manipulators in C++ is used to Turns off the unit buff flag nouppercase: The nouppercase manipulator of stream manipulators in C++ is used to Turns off the uppercase flag oct: The oct manipulator of stream manipulators in C++ is used to Turns on oct flag resetiosflags(fmtflags f): The resetiosflags manipulator of stream manipulators in C++ is used to Turns off the flag specified in f right: The right manipulator of stream manipulators in C++ is used to Turns on the right flag scientific: The scientific manipulator of stream manipulators in C++ is used to Turns on scientific flag setbase(int base): The setbase manipulator of stream manipulators in C++ is used to Set the number base to base setfill(int ch): The setfill manipulator of stream manipulators in C++ is used to Set the fill character to ch setiosflags(fmtflags f): The setiosflags manipulator of stream manipulators in C++ is used to Turns on the flag specified in f setprecision(int p): The setprecision manipulator of stream manipulators in C++ is used to Set the number of digits of precision setw(int w): The setw manipulator of stream manipulators in C++ is used to Set the field width to w showbase: The showbase manipulator of stream manipulators in C++ is used to Turns on showbase flag showpoint: The showpoint manipulator of stream manipulators in C++ is used to Turns on show point flag showpos: The showpos manipulator of stream manipulators in C++ is used to Turns on showpos flag skipws: The skipws manipulator of stream manipulators in C++ is used to Turns on skipws flag unitbuf: The unitbuf manipulator of stream manipulators in C++ is used to turn on unitbuf flag uppercase: The uppercase manipulator of stream manipulators in C++ is used to turn on the uppercase flag ws: The ws manipulator of stream manipulators in C++ is used to skip leading white space To access manipulators that take parameters (such as setw( )), you must include "iomanip" header file in your program. Example: CPP #include <iomanip> #include <iostream> using namespace std; void Example() { // performs ssame as setf( ) cout << setiosflags(ios::showpos); cout << 123<<"\n"; // hexadecimal base (i.e., radix 16) cout << hex << 100 << endl; // Set the field width cout << setfill('*') << setw(10) << 2343.0; } int main() { Example(); return 0; } Output: +123 64 *****+2343 The manipulator setiosflags( ) . Comment More infoAdvertise with us Next Article Date and Time Parsing in C++ S Somil_singh Follow Improve Article Tags : C++ cpp-input-output Practice Tags : CPP Similar Reads C++ 20 - std::format C++20 comes with a host of new features and improvements, including the format() function. In this article, we will explore how std::format can be used to format strings in C++20. C++20 - std::format std::format is a new function Introduced in C++20 that provides a way to format strings by replacing 4 min read cout in C++ In C++, cout is an object of the ostream class that is used to display output to the standard output device, usually the monitor. It is associated with the standard C output stream stdout. The insertion operator (<<) is used with cout to insert data into the output stream.Let's take a look at 2 min read Date and Time Parsing in C++ The Date and time parsing is a common task in programming especially when dealing with user input or data from external sources. C++ provides various utilities and libraries to handle date and time parsing efficiently. Some of the most commonly used libraries for date and time parsing in C++ are: 5 min read I/O Redirection in C++ In C++, input and output operations are done in the form of sequence of bytes called stream through the stream objects like cin and cout. These objects use different components such as buffers, streams, etc. to efficiently read and write data to standard input and output devices such as keyboard and 6 min read File Handling through C++ Classes In C++, programs run in the computerâs RAM (Random Access Memory), in which the data used by a program only exists while the program is running. Once the program terminates, all the data is automatically deleted. File handling allows us to manipulate files in the secondary memory of the computer (li 8 min read Basic Input / Output in C++ In C++, input and output are performed in the form of a sequence of bytes or more commonly known as streams.Input Stream: If the direction of flow of bytes is from the device (for example, Keyboard) to the main memory then this process is called input.Output Stream: If the direction of flow of bytes 5 min read Like