
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are C++ Manipulators (endl, setw, setprecision, setf)?
Manipulators in C++ are like helper functions that are designed to format and modify the input and output streams of your code. This is done by using the insertion (<<) and extraction (>>) operators, and are defined in <iostream> and <iomanip> header files.
There are various types of manipulators that exist in C++, but in this article, we will be discussing the four main types, which are commonly used.
C++ endl Manipulator
The endl manipulator has the same functionality as '\n'(newline character - telling the program to move the cursor to the next line). But this also flushes the output stream.
Therefore, this is used to insert the newline character (\n) and flushes the output stream. It is defined inside the <iostream> header file.
Syntax
The following is the syntax of using endl, which is further followed by a semicolon (;) in C++.
cout << variable_name << endl;
Example
This is the following example showcasing the use of endl in a C++ program to terminate a statement and print output on separate lines:
#include <iostream> using namespace std; int main() { int n = 2016; cout << n << endl; cout << "Hello" << endl; cout << "Tutorialspoint" << endl << "Learner"; return 0; }
Output
2016 Hello Tutorialspoint Learner
C++ setw Manipulators
The setw stands for set width which is defined inside the <iomanip> header file. It is used to change the width of the next input/output field. When it is used in an expression like out << setw(n) or in >> setw(n). Then it sets the width parameter of the in and out stream equal to n.
Syntax
This is the following syntax of setw manipulator, here n is the value of field width:
cout << setw(n) << value;
Example
This is the following example of setw, showcasing how field width is set for each output:
#include <iostream> #include <iomanip> // for setw using namespace std; int main() { cout << setw(5) << "Sport" << setw(5) << "Players" << endl; cout << setw(10) << "Cricket" << setw(10) << 11 << endl; cout << setw(15) << "Football" << setw(15) << 11 << endl; cout << setw(20) << "Basketball" << setw(20) << 5 << endl; return 0; }
Output
SportPlayers Cricket 11 Football 11 Basketball 5
C++ setprecision Manipulator
The setprecision manipulator is defined inside the <iomanip> header file, which is used to change floating-point precision (meaning the number of digits will display for floating-point numbers) or set precision.
When it is used in an expression like out << setprecision(n) or in >> setprecision(n), it sets the precision parameter of the out and in stream equal to n.
Syntax
This is the following syntax of setprecision, here n specifies the number of digits to be display for the given number:
cout << setprecision(n) << number;
Example
This is the following example showcasing how setprecision is used to display the number of digits user wants to display:
#include <iostream> #include <iomanip> using namespace std; int main() { double num = 123.456789; cout << "num value: " << num << endl; cout << "setprecision(4): " << setprecision(4) << num << endl; cout << "setprecision(6): " << setprecision(6) << num << endl; return 0; }
Output
num value: 123.457 setprecision(4): 123.5 setprecision(6): 123.457
C++ setf Manipulator
The setf is a member function that calls stream objects to set formatting flags. And manipulators like fixed, showpoint, setw are special objects that internally call the stef() function. So let's see with an example showpoint and noshowpoint. This manipulator controls whether the decimal point is always included in the floating-point representation.
Syntax
Here is the following example of showpoint and noshowpoint to control the display of decimal points of floating-value.
cout << showpoint << value; cout << noshowpoint << value;
Example
This is the following example showcasing how showpoint allows to display the decimal digits of floating-value, whereas noshowpoint don't allows to diplay the decimal digits of floating-value:
#include <iostream> using namespace std; int main() { cout << "1.0 with showpoint: " << showpoint << 1.0 << endl; cout << "1.0 with noshowpoint: " << noshowpoint << 1.0; return 0; }
Output
1.0 with showpoint: 1.00000 1.0 with noshowpoint: 1