
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Manipulators
- C++ Modifier Types
- C++ Storage Classes
- C++ Constexpr Specifier
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Scope Resolution Operator
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ Design Patterns
- C++ Creational Design Patterns
- C++ Singleton Design Pattern
- C++ Factory Method Design Pattern
- C++ Abstract Factory Pattern
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
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
- Input Stream Manipulators
- Alignment Manipulators
- Floating Point Manipulators
- Numeric Base and Case Manipulators
- Boolean Manipulators
- Time and Date Manipulators
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 −
Manipulators | Definition | Syntax |
---|---|---|
endl | It 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; |
showpoint | It displays decimal point for floating point numbers. | cout << showpoint << 7.0; |
noshowpoint | It hides the decimal point. | cout << noshowpoint << 7.0; |
showpos | It is used for displaying '+' sign for positive numbers. | cout << showpos << 57; |
noshowpos | It hides '+' sign for positive numbers. | cout << noshowpos << 57; |
flush | It flushes the output stream without newline. | cout << "Data" << flush; |
ends | It 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> using namespace std; int main(){ 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; return 0; }
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.
Manipulators | Definition | Syntax |
---|---|---|
ws | It removes the whitespace character. | cin >> ws >> str; |
noskipws | It avoids skipping whitespaces. | cin >> noskipws >> ch; |
The following example shows the application of input stream manipulators ws and noskipws −
#include <iostream> #include <sstream> using namespace std; int main() { 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; return 0; }
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.
Manipulators | Definition | Syntax |
---|---|---|
left | It left aligns the output within field width. | cout << left << setw(10) << "Hi"; |
right | It right aligns the output within field width. | cout << right << setw(10) << "Hi"; |
internal | It 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> using namespace std; int main() { 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; return 0; }
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.
Manipulators | Definition | Syntax |
---|---|---|
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; |
fixed | It represents the given number in fixed-point notation. | cout << fixed << 1.41421356; |
scientific | It 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> using namespace std; int main(){ 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; return 0; }
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.
Manipulators | Definition | Syntax |
---|---|---|
dec | It sets the given value as decimal output. | cout << dec << 57; |
oct | It sets the given value as octal output. | cout << oct << 57; |
hex | It sets the given value as hexadecimal output. | cout << hex << 57; |
setbase | It is used for setting the numeric base as decimal, octal, or hexadecimal in the output. | cout << setbase(16) << 42; |
showbase | It is used to display the base prefix in the output. | cout << showbase << hex << 42; |
noshowbase | It hides base prefix in the output. | cout << noshowbase << hex << 42; |
uppercase | It represents hex and scientific in uppercase letters. | cout << uppercase << hex << 255; |
nouppercase | It 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> using namespace std; int main(){ 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; return 0; }
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.
Manipulators | Definition | Syntax |
---|---|---|
boolalpha | It displays booleans as true/false. | cout << boolalpha << true; |
noboolalpha | It 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> using namespace std; int main(){ 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; return 0; }
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.
Manipulators | Definition | Syntax |
---|---|---|
put_time | It is used to format and output time in the specified format. | cout << put_time(&tm, "%Y-%m-%d"); |
get_time | It 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> using namespace std; int main(){ 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"); return 0; }
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.