What is manipulator in C++?

In C++, a manipulator is a function that is used to modify the input or output formatting of a stream (such as std::cin or std::cout). Manipulators can be used to control how data is read from or written to the console, making them useful for formatting tasks like adjusting the width of an output, aligning text, or setting the precision for floating-point numbers.

Types of Manipulators in C++

1. Standard Manipulators

These are built-in manipulators that do not require arguments.

  • endl: Inserts a newline character and flushes the output buffer.

    std::cout << "Hello" << std::endl; // Outputs "Hello" followed by a newline
  • setw: Sets the width of the next input/output operation.

    std::cout << std::setw(10) << "Hi"; // Outputs "Hi" padded to 10 characters
  • setprecision: Sets the number of digits after the decimal point for floating-point numbers.

    std::cout << std::setprecision(3) << 3.14159; // Outputs "3.14"
  • fixed and scientific: Used to display floating-point numbers in fixed-point or scientific notation.

    std::cout << std::fixed << 123.456; // Outputs "123.456" std::cout << std::scientific << 123.456; // Outputs "1.234560e+02"

2. Custom Manipulators

You can also create custom manipulators to define specific behaviors or formats for input/output operations.

Example:

std::ostream& customEndl(std::ostream& out) { out << "\n--- End of Line ---\n"; return out; } std::cout << "Hello" << customEndl; // Outputs "Hello" followed by a custom message

Commonly Used Manipulators:

  • hex, oct, and dec: Used to change the number base (hexadecimal, octal, or decimal) for integer output.

    std::cout << std::hex << 255; // Outputs "ff"
  • showpos: Displays the positive sign for numbers.

    std::cout << std::showpos << 42; // Outputs "+42"
  • left, right, internal: Aligns output to the left, right, or internally within the specified width.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What are your biggest challenges in your career?
What is Coinbase domain interview?
What are SQL query optimization interview questions?
What is the dress code for Palantir office?
Which country is best for tech jobs in 2024?
What are the 7 problem-solving strategies?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.