Manipulators in C++

Manipulators in C++ are special functions or objects that are used with insertion (<<) and extraction (>>) operators (cin/cout) to control the input and output streams. You can change the format to display the output or how to read an input. The manipulators are declared in <iostream> and <iomanip> headers.

Types of Manipulators in C++

The manipulators can be classified into following categories −

Output Stream Manipulators

The output stream manipulators are used to control display properties such as field width, fill characters, and numeric display options. The below lists all the output stream manipulators −

ManipulatorsDefinitionSyntax
endlIt inserts a newline and flushes the output buffer.cout << “Tutorials” << endl;
setw(n)It sets the field width for the next output operation.cout << setw(5) << 57;
setfill()It sets the fill character.cout << setfill(‘*’) << setw(5) << 42;
showpointIt displays decimal point for floating point numbers.cout << showpoint << 7.0;
noshowpointIt hides the decimal point.cout << noshowpoint << 7.0;
showposIt is used for displaying ‘+’ sign for positive numbers.cout << showpos << 57;
noshowposIt hides ‘+’ sign for positive numbers.cout << noshowpos << 57;
flushIt flushes the output stream without newline.cout << “Data” << flush;
endsIt is used for inserting a null character and then flushes.cout << “String” << ends;

Here is an example illustrating the use of some of the output stream manipulators in C++ −

#include <iostream>#include <iomanip>usingnamespace std;intmain(){

    cout <<"C++ Output Manipulators Examples:"<< endl;
    cout <<"\nThis is the first line"<< endl;
    cout <<"This is the second line using endl"<< endl;
    cout << endl;

    cout <<setw(5)<<57<<" (Field width 5) using setw()"<< endl;
    cout <<setw(10)<<40<<" (Field width 10) using setw()"<< endl;

    cout <<"\n";
    cout <<setfill('*')<<setw(8)<<42<<" Filled with * using setfill()"<< endl;
    cout <<setfill('-')<<setw(8)<<123<<" Filled with - using setfill()"<< endl;
    cout <<setfill(' ');// Reset to space

    cout <<"\nDecimal Point Display:"<< endl;
    cout <<"With showpoint: "<< showpoint <<7.0<<", "<<3.0<< endl;
    cout <<"With noshowpoint: "<< noshowpoint <<7.0<<", "<<3.0<< endl;return0;}

The output of the above code is as follows −

C++ Output Manipulators Examples:

This is the first line
This is the second line using endl

   57 (Field width 5) using setw()
        40 (Field width 10) using setw()

******42 Filled with * using setfill()
-----123 Filled with - using setfill()

Decimal Point Display:
With showpoint: 7.00000, 3.00000
With noshowpoint: 7, 3

Input Stream Manipulators

The input manipulators control whitespaces and also handles input stream during read operations.

ManipulatorsDefinitionSyntax
wsIt removes the whitespace character.cin >> ws >> str;
noskipwsIt avoids skipping whitespaces.cin >> noskipws >> ch;

The following example shows the application of input stream manipulators ws and noskipws −

#include <iostream>#include <sstream> usingnamespace std;intmain(){
    string text ="   A B";   
    cout <<"Original Text: "<< text << endl;
    
    istringstream iss(text);char ch;

    iss >> ws >> ch;
    cout <<"Using ws: '"<< ch <<"'"<< endl;// Reset stream
    iss.clear();
    iss.seekg(0);
    
    iss >> noskipws >> ch;
    cout <<"Using noskipws: '"<< ch <<"'"<< endl;return0;}

The output of the above code is as follows. Here ws skips all the white spaces while noskipws prints the white space without skipping it.

Original Text:    A B
Using ws: 'A'
Using noskipws: ' '

Alignment Manipulators

Alignment manipulators are used to control alignment of output within its field width.

ManipulatorsDefinitionSyntax
leftIt left aligns the output within field width.cout << left << setw(10) << “Hi”;
rightIt right aligns the output within field width.cout << right << setw(10) << “Hi”;
internalIt is used to insert padding between sign and value.cout << internal << setw(6) << -57;

In this example, we have used left and right manipulators that aligns the number to left and right respectively while internal sets the alignment between ‘-‘ and number.

#include <iostream>#include <iomanip>usingnamespace std;intmain(){int num =123;int negNum =-123;

    cout <<"Left aligned   : '"<< left <<setw(5)<< num <<"'"<< endl;
    cout <<"Right aligned  : '"<< right <<setw(5)<< num <<"'"<< endl;
    cout <<"Internal align : '"<< internal <<setw(5)<< negNum <<"'"<< endl;return0;}

The output of the above code is as follows:

Left aligned   : '123  '
Right aligned  : '  123'
Internal align : '- 123'

Floating Point Manipulators

Floating point manipulators are used to control the decimal precision and set the notation to represent the decimal numbers.

ManipulatorsDefinitionSyntax
setprecision(n)It sets the precision for decimal output. It specifies upto how many decimal points, we want the output.cout << setprecision(2) << 1.41421356;
fixedIt represents the given number in fixed-point notation.cout << fixed << 1.41421356;
scientificIt represents the given number in scientific notation.cout << scientific << 1234.5;

The following example sets the precision of given number using setprecision(). The fixed and scientific gives result upto 6 decimal places if setprecision() manipulator is not used.

#include <iostream>#include <iomanip>usingnamespace std;intmain(){double num =1.41421356;
    cout <<"Original number:"<< num << endl;
    cout <<"Default: "<<setprecision(5)<< num 
         << endl;
    cout <<"Fixed: "<< fixed <<setprecision(3)<< num << endl;
    cout <<"Scientific: "<< scientific <<setprecision(3)<< num << endl;return0;}

The output of the above code is as follows:

Original number:1.41421
Default: 1.4142
Fixed: 1.414
Scientific: 1.414e+00

Numeric Base and Case Manipulators

The numeric base manipulators are used to convert and set the base of the given number. Case manipulators specifies whether you want your hex and scientific output in uppercase or lowercase.

ManipulatorsDefinitionSyntax
decIt sets the given value as decimal output.cout << dec << 57;
octIt sets the given value as octal output.cout << oct << 57;
hexIt sets the given value as hexadecimal output.cout << hex << 57;
setbaseIt is used for setting the numeric base as decimal, octal, or hexadecimal in the output.cout << setbase(16) << 42;
showbaseIt is used to display the base prefix in the output.cout << showbase << hex << 42;
noshowbaseIt hides base prefix in the output.cout << noshowbase << hex << 42;
uppercaseIt represents hex and scientific in uppercase letters.cout << uppercase << hex << 255;
nouppercaseIt represents hex and scientific in lowercase letters.cout << nouppercase << hex << 255;

The following example demonstrates the use of base and case manipulators where numeric base manipulators sets or converts the base of the given number into decimal, hex or oct. The case manipulator converts the given value into lower case and upper case.

#include <iostream>#include <iomanip>usingnamespace std;intmain(){int number =255;
     cout <<"Original number:"<< number << endl;

     cout <<"Decimal: "<< dec << number <<", "<<"Octal: "<< oct << number <<", "<<"Hex: "<< hex << number << endl;// Reset to decimal
     cout << dec;

     cout <<"\nSetting base using setbase():"<< endl;
     cout <<"Base 10: "<<setbase(10)<< number <<", "<<"Base 8: "<<setbase(8)<< number <<", "<<"Base 16: "<<setbase(16)<< number << endl;

     cout <<"\nHiding base prefix using noshowbase:"<< endl;
     cout << noshowbase;
     cout <<"Decimal: "<< dec << number <<", "<<"Octal: "<< oct << number <<", "<<"Hex: "<< hex << number << endl;

     cout <<"\nDisplaying base prefix using showbase:"<< endl;
     cout << showbase;
     cout <<"Decimal: "<< dec << number <<", "<<"Octal: "<< oct << number <<", "<<"Hex: "<< hex << number << endl;

     cout <<"\nLowercase hex using nouppercase: "<< nouppercase 
          << hex << showbase <<255<<", "<<171<< endl;
     cout <<"Uppercase hex uppercase: "<< uppercase << hex 
          << showbase <<255<<", "<<171<< endl;return0;}

The output of the above code is as follows:

Original number:255
Decimal: 255, Octal: 377, Hex: ff

Setting base using setbase():
Base 10: 255, Base 8: 377, Base 16: ff

Hiding base prefix using noshowbase:
Decimal: 255, Octal: 377, Hex: ff

Displaying base prefix using showbase:
Decimal: 255, Octal: 0377, Hex: 0xff

Lowercase hex using nouppercase: 0xff, 0xab
Uppercase hex uppercase: 0XFF, 0XAB

Boolean Manipulators

Boolean manipulators are used to represent the boolean values as either true/false or 0/1.

ManipulatorsDefinitionSyntax
boolalphaIt displays booleans as true/false.cout << boolalpha << true;
noboolalphaIt displays booleans as 1/0.cout << noboolalpha << true;

Here is an example demonstrating the use of boolean manipulators where boolalpha and noboolalpha display boolean values as true/false and 1/0, respectively.

#include <iostream>#include <iomanip>usingnamespace std;intmain(){
    cout <<"Using boolalpha for comparison"<< endl;int a =10, b =20, c =10;

    cout << boolalpha;
    cout <<"a = "<< a <<", b = "<< b <<", c = "<< c << endl;
    cout <<"a == b: "<<(a == b)<<", "<<"a == c: "<<(a == c)<<", "<<"a < b: "<<(a < b)<<", "<<"b > c: "<<(b > c)<< endl;

    cout <<"\nUsing noboolalpha for comparison"<< endl;
    cout << noboolalpha;
    cout <<"a = "<< a <<", b = "<< b <<", c = "<< c << endl;
    cout <<"a == b: "<<(a == b)<<", "<<"a == c: "<<(a == c)<<", "<<"a < b: "<<(a < b)<<", "<<"b > c: "<<(b > c)<< endl;return0;}

The output of the above code is as follows:

Using boolalpha for comparison
a = 10, b = 20, c = 10
a == b: false, a == c: true, a < b: true, b > c: true

Using noboolalpha for comparison
a = 10, b = 20, c = 10
a == b: 0, a == c: 1, a < b: 1, b > c: 1

Time and Date Manipulators

You can format the time in the given format or parse the formatted time and date using time and date manipulators.

ManipulatorsDefinitionSyntax
put_timeIt is used to format and output time in the specified format.cout << put_time(&tm, “%Y-%m-%d”);
get_timeIt is used for parsing the formatted time input.cin >> get_time(&tm, “%Y-%m-%d”);

The following example demonstrates time and date manipulators to format and parse the date and time using put_time and get_time respectively:

#include <iostream>#include <iomanip>#include <sstream>#include <ctime>usingnamespace std;intmain(){
     time_t now =time(0);
     tm *ltm =localtime(&now);

     cout <<"Current date and time using put_time: "<<"\n";
     cout <<put_time(ltm,"%Y-%m-%d %H:%M:%S")<< endl;

     string dateStr ="2025-09-08 14:30:00";
     tm t ={};
     istringstream ss(dateStr);

     cout <<"Custom date and time using get_time: "<<"\n"<< dateStr << endl;
     ss >>get_time(&t,"%Y-%m-%d %H:%M:%S");return0;}

The output of the above code is as follows:

Current date and time using put_time: 
2025-09-08 07:19:03
Custom date and time using get_time: 
2025-09-08 14:30:00

Conclusion

Manipulators in C++ are special functions that are used with input and output streams (cin and cout) to change the output format and how to read an input. Manipulators in C++ can be further categorized into the following types: output stream manipulators, input stream manipulators, alignment manipulators, floating point manipulators, numeric base and case manipulators, boolean manipulators, time and date manipulators.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *