VOOZH about

URL: https://www.geeksforgeeks.org/cpp/manipulators-in-c-with-examples/

⇱ Manipulators in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Manipulators in C++

Last Updated : 17 Jan, 2026

A manipulator is a special function in C++ that is used with input/output streams (like cin, cout ) to change the way data is shown or read. We can use manipulators when we want to :

  • Format numbers
  • Set precision (decimals)
  • Align text
  • Change number base (like decimal to hexadecimal)
  • Control spacing

They are most commonly used with cout for output formatting.

Types of Manipulators

There are various types of manipulators classified based on the type of entity they manipulate:

1. Output Stream Manipulators

These manipulators format how data is shown on the screen. The following table lists some common output stream manipulators:

Manipulator

What it Does

Example Usage

endl

Inserts a newline and flushes output

cout<<"Hello"<<endl;

flush

Flushes the output buffer immediately

cout<<flush;

setw(n)

Sets width of the next output field (spaces used for padding)

cout<<setw(10)<<42;

setprecision(n)

Sets the number of digits after decimal point

cout<< setprecision(3)<<3.14159;

fixed

Uses fixed-point notation for floating numbers.

cout<<fixed<<setprecision(2)<<3.14;

scientific

Uses scientific notation (exponential)

cout<< scientific << 1234.5;

showpoint

Always shows the decimal point (even if no digits after decimal)

cout<< showpoint<< 2.0;

noshowpoint

Hides unnecessary decimal point

cout<< noshowpoint<< 2.0;

Example:


Output
Hello
 42
3.14
3.142
3.142e+00
4.200e+01

2. Input Stream Manipulators

These manipulators help control how data is read from input streams. Following table lists some common input stream manipulators:

Manipulator

What it Does

Example Usage

ws

Skips all leading whitespace characters (spaces, tabs, newlines) before reading input

cin>>ws>>someString;

noskipws

Stops skipping whitespace (reads every character including spaces)

cin>> noskipws >> ch;

Example:


Input

s x

Output

c1: s, c2: 

3. Boolean Manipulators

These manipulators control how boolean values (true/false) are displayed. Following table lists some common boolean manipulators:

Manipulator

What it Does

Example Usage

boolalpha

Prints true or false instead of 1 or 0.

cout<< boolalpha<< true;

noboolalpha

Prints 1 or 0 instead of true/ false

cout<< noboolalpha<< true;

Example:


Output
true
1

4. Alignment and Sign Manipulators

These control the alignment of output and how signs are shown. Following table lists some common alignment and sign manipulators:

Manipulator

What it Does

Example Usage

left

Left-aligns output in the given width

cout<< left<< setw(10) << 42;

right

Right-aligns output (default for numbers)

cout<< right<< setw(10)<< 42;

internal

Places the sign to the left, padding after the sign

cout<< internal << setw(10) << -42;

showpos

Shows a + sign for positive numbers

cout << showpos << 42;

noshowpos

Hides the + sign for positive numbers

cout<< noshowpos <<42;

Example:


Output
42 
 42
+42
42

5. Base Manipulators

These manipulators change the number base used for integer output. Following table lists some common base manipulators:

Manipulator

What it Does

Example Usage

dec

Prints numbers in decimal (base 10)

cout << dec << 255;

hex

Prints numbers in hexadecimal (base 16)

cout << hex << 255;

oct

Prints numbers in octal (base 8)

cout << oct <<255;

Example:


Output
2a
42
52
Comment