C++ Manipulators are used to format the output in different styles. The manipulators are the most common way to control output formatting.
Some Important Manipulators in C++ are as follows:
- Endl
- The Endl manipulators can be used by including iostream.h file in the program.
- Setw, Setprecision, Setfill, Showpoint, Fixed
- These manipulators can be used by including iomanip.h file.
Concept of C++ Manipulators:
Manipulators in C++ are special functions or objects that you can use to control the formatting of output on the screen. They allow you to tweak various important aspects of how your data is displayed from controlling the number of decimal places to setting the width of columns in your output. C++ Manipulators are important tools for making your output look neat and organized.
Table of Contents
ToggleEndl: Break the Line or Shift To New Line
- Meaning: endl means “end line.” It is like the pressing the enter key on a keyboard making the text jump to the next line.
- Full Form: End Line
- Use: To move to the next line in the output.
- Significant Point: Creates a new line.
- Example: cout << “Hello” << endl << “World”;
Output:
Hello
World
Setw: Setting the Width
- Meaning: setw means “set width.” It tells the computer how much space to leave for the next piece of text.
- Full Form: Set Width
- Use: To specify how wide a field should be when printing.
- Key Aspect: Determines the space allocated for text or numbers.
- Example: cout << setw(10) << “Hello”;
Output:
Hello
Setprecision: Precision Control
- Meaning: setprecision means controlling how precise something is. It tells the computer how many digits to show after the decimal point.
- Full Form: Set Precision
- Use: To decide how many decimal places to display for numbers.
- Vital Aspect: Controls the precision of floating-point numbers.
- Example: cout << setprecision(3) << 3.14159;
Output:
3.14
Setfill: Filling the Gaps
- Meaning: setfill means filling in the empty spaces. It tells the computer what character to use to fill the empty space.
- Full Form: Set Fill
- Use: To choose a character to fill empty spaces when setting the width.
- Essential Point: Determines the filling character for setw.
- Example: cout << setw(10) << setfill(‘*’) << “Hello”;
Output:
*****Hello
Showpoint: Always Showing the Dot
- Meaning: showpoint means showing the decimal point. It tells the computer to always show the decimal point even if there are no numbers after it.
- Full Form: Show Point
- Use: To always display the decimal point for numbers.
- Crucial Aspect: Always shows the decimal point for clarity.
- Example: cout << showpoint << 10.0;
Output:
10.0
Fixed: Fixed-Point Notation
- Meaning: fixed means staying the same. It tells the computer to always show numbers in a fixed format, without scientific notation.
- Full Form: Fixed
- Use: To display floating-point numbers without scientific notation.
- Key Feature: Prevents the use of scientific notation for numbers.
- Example: cout << fixed << 3.14;
Output:
3.140000
Recommended Article: