Table of Contents
By default, when the field width is greater than the size of the output value, the remaining positions are filled with spaces. C++ allows the fill character to be changed using the std::setfill() manipulator provided by the <iomanip> library.
The fill character remains active for all subsequent output operations until another fill character is specified. It is commonly used when formatting tables, reports, and decorative console output.
Source Code #
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setfill('*');
std::cout << std::setw(10) << 25 << '\n';
std::cout << std::setw(10) << 350 << '\n';
std::cout << std::setw(10) << 7890 << '\n';
return 0;
}
Output #
********25
*******350
******7890
Explanation #
#include <iomanip>includes the header that provides stream formatting manipulators.std::setfill('*')changes the fill character from the default space to an asterisk (*).std::setw(10)specifies that the next output value should occupy a minimum field width of 10 characters.- If the output value is shorter than the specified field width, the remaining positions are filled with the current fill character.
- Unlike
std::setw(), which affects only the next output operation,std::setfill()remains in effect until another fill character is specified. - The stored values are not modified; only their displayed representation changes.
std::coutdisplays the formatted output on the console.return 0;terminates the program successfully.