0% found this document useful (0 votes)
7 views

Manipulators in C

The document explains manipulators in C++, which are functions that modify input/output stream formats. It categorizes manipulators into output stream, input stream, boolean, alignment and sign, and base manipulators, providing examples for each type. The document also includes code snippets demonstrating the use of various manipulators to format output and input effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Manipulators in C

The document explains manipulators in C++, which are functions that modify input/output stream formats. It categorizes manipulators into output stream, input stream, boolean, alignment and sign, and base manipulators, providing examples for each type. The document also includes code snippets demonstrating the use of various manipulators to format output and input effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Manipulators in C++



Manipulators are helping functions that can modify the stream. They
can be included in the I/O statement to alter the format parameters of a
stream. They are defined inside <iomanip> and some are also defined
inside <iostream> header file.
For example, if we want to print the hexadecimal value of 100 then we
can print it as:
cout << setbase(16) << 100
Types of Manipulators
There are various types of manipulators classified on the basis type of
entity they manipulate:
Table of Content
1. Output Stream Manipulators
Output stream manipulators are used to control and format the output
stream, such as setting the width, precision, or alignment of printed
data. They allow for a better presentation of output.
Following table lists some common output stream manipulators:
Header
Manipulator Description File

Inserts a newline and flushes the


endl iostream
output stream.

flush Flushes the output stream manually. iostream

Sets the width of the next output field


setw(x) iomanip
to x.

Sets the precision for floating-point


setprecision(x) iomanip
numbers to x.

Displays numbers in fixed-point


fixed iomanip
notation.
Header
Manipulator Description File

Displays numbers in scientific


scientific iomanip
notation.

Forces the display of the decimal


showpoint iomanip
point.

Hides the decimal point unless


noshowpoint iomanip
necessary.
Example
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// Output a new line and flush the stream
cout << "Hello" << endl;
// Set width to 10 for the next output
cout << setw(10) << 42 << endl;
// Set precision to 3 for floating-point numbers
cout << setprecision(3) << 3.14159 << endl;
// Use fixed-point notation
cout << fixed << 3.14159 << endl;
// Use scientific notation
cout << scientific << 3.14159 << endl;
// Show the decimal point even for whole numbers
cout << showpoint << 42.0;
return 0;
}
Output
Hello
42
3.14
3.142
3.142e+00
4.200e+01
2. Input Stream Manipulators
Input stream manipulators are used to modify the behaviour of the
input stream. They help in processing input efficiently, such as skipping
unnecessary whitespaces with ws.
Following table lists some common input stream manipulators:
Header
Manipulator Description File

Skips leading whitespaces in the input


ws iostream
stream.

Disables skipping of leading


noskipws iostream
whitespaces.

Example
#include <iostream> using namespace std;
int main() {
char c1, c2;
// Input skips whitespace by default
cin >> c1;
// Input the next character without skipping whitespace
cin >> noskipws >> c2;
cout << "c1: " << c1 << ", c2: " << c2;
return 0; }
3. Boolean Manipulators
Boolean manipulators are used to format boolean values in output.
They allow displaying boolean values as true or false or as 1 and 0,
depending on the requirement.

Following table lists some common boolean manipulators:


Header
Manipulator Description File

Displays true or false for boolean


boolalpha iostream
values.

noboolalpha Displays 1 or 0 for boolean values. iostream


Header
Manipulator Description File

Example

#include <iostream>

using namespace std;

int main() {

bool value = true;

// Display boolean as true/false

cout << boolalpha << value << endl;

// Display boolean as 1/0

cout << noboolalpha << value;


return 0; }
Header
Manipulator Description File
4.

left Aligns output to the left. iomanip

right Aligns output to the right. iomanip

Aligns signs and base prefixes to


internal iomanip
the left.

Displays a + sign for positive


showpos iostream
numbers.

Hides the + sign for positive


noshowpos iostream
numbers.

Alignment and Sign Manipulators


These manipulators control how text and numbers are aligned or how
their signs are displayed in the output.
Following table lists some common alignment and sign
manipulators:

Example
#include <iostream> #include <iomanip> using namespace std;
int main() {
int n = 42;
// Align output to the left
cout << left << setw(10) << n << endl;
// Align output to the right
cout << right << setw(10) << n << endl;
// Show positive sign for numbers
cout << showpos << n << endl;
// Don't show positive sign for numbers
cout << noshowpos << n;
return 0; }
5. Base Manipulators
Base manipulators are used to format numbers in different bases, such
as decimal, hexadecimal, or octal. They help in representing numbers
in a way suited to specific applications.
Following table lists some common base manipulators:
Manipulator Description Header File

hex Formats output in hexadecimal base. iostream

dec Formats output in decimal base. iostream

oct Formats output in octal base. iostream

Example
#include <iostream> using namespace std;
int main() {
int n = 42;
// Output in hexadecimal base
cout << hex << n << endl;
// Output in decimal base
cout << dec << n << endl;
// Output in octal base
cout << oct << n;
return 0; }
Output
2a
42
52

You might also like