C++ Manipulators | Endl, Setw, Setprecision, Setfill, Showpoint, Fixed

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:

  1. Endl
  2. The Endl manipulators can be used by including iostream.h file in the program.
  3. Setw, Setprecision, Setfill, Showpoint, Fixed
  4. These manipulators can be used by including iomanip.h file.
C++ Manipulators

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.

Endl: Break the Line or Shift To New Line

  1. Meaning: endl means “end line.” It is like the pressing the enter key on a keyboard making the text jump to the next line.
  2. Full Form: End Line
  3. Use: To move to the next line in the output.
  4. Significant Point: Creates a new line.
  5. Example: cout << “Hello” << endl << “World”;

Output:

Hello

World

Setw: Setting the Width

  1. Meaning: setw means “set width.” It tells the computer how much space to leave for the next piece of text.
  2. Full Form: Set Width
  3. Use: To specify how wide a field should be when printing.
  4. Key Aspect: Determines the space allocated for text or numbers.
  5. Example: cout << setw(10) << “Hello”;

Output:

 Hello

Setprecision: Precision Control

  1. Meaning: setprecision means controlling how precise something is. It tells the computer how many digits to show after the decimal point.
  2. Full Form: Set Precision
  3. Use: To decide how many decimal places to display for numbers.
  4. Vital Aspect: Controls the precision of floating-point numbers.
  5. Example: cout << setprecision(3) << 3.14159;

Output:

3.14

Setfill: Filling the Gaps

  1. Meaning: setfill means filling in the empty spaces. It tells the computer what character to use to fill the empty space.
  2. Full Form: Set Fill
  3. Use: To choose a character to fill empty spaces when setting the width.
  4. Essential Point: Determines the filling character for setw.
  5. Example: cout << setw(10) << setfill(‘*’) << “Hello”;

Output:

*****Hello

Showpoint: Always Showing the Dot

  1. 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.
  2. Full Form: Show Point
  3. Use: To always display the decimal point for numbers.
  4. Crucial Aspect: Always shows the decimal point for clarity.
  5. Example: cout << showpoint << 10.0;

Output:

10.0

Fixed: Fixed-Point Notation

  1. Meaning: fixed means staying the same. It tells the computer to always show numbers in a fixed format, without scientific notation.
  2. Full Form: Fixed
  3. Use: To display floating-point numbers without scientific notation.
  4. Key Feature: Prevents the use of scientific notation for numbers.
  5. Example: cout << fixed << 3.14;

Output:

3.140000

Recommended Article:


How To End An If Statement In Python | 5 Methods

Leave a Reply

Your email address will not be published. Required fields are marked *