Home C++ Iostream Functions right C++ right Manipulator Definition In C++, right sets the alignment of a stream to the right. When a value is shorter than the field width from setw, the padding goes on the left, so the text sits at the right edge.
This is the default alignment, so you need right mostly to switch back after left. It is the natural choice for numbers in a column. It comes from <ios>, and setw comes from <iomanip>.
This is a manipulator . You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).
Syntax Put std::right into the stream and combine it with std::setw.
#include <iomanip>
std::cout << std::right << std::setw(8) << "ab"; // ab Call What it does std::cout << std::rightRight alignment from now on. std::cout << std::right << std::setw(8) << 42Prints 42 with six spaces in front. std::cout << std::leftGoes to left alignment.
Right alignment plus setfill('0') is the usual way to print numbers like 00042.
Library declarations ios_base& right(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << right. You never write this argument yourself.
Return Value Return Value Description ios_base&A reference to the same stream, so you can keep chaining << or >> after right.
C++ Examples 1. Switch back 2. Zero padding 3. A small table Example 1 — Go Back to Right Alignment Use left first, then right to return.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::left << std::setw(8) << "ab" << '|' << '\n';
std::cout << std::right << std::setw(8) << "ab" << '|' << '\n';
return 0;
} How It Works left puts the padding after the text, so ab stays at the left edge.right turns that off and puts the padding in front of the text.Both fields are eight characters wide. The bar | shows where each field ends. Example 2 — Pad a Number with Zeros Right alignment with setfill('0') gives numbers a fixed length.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::right << std::setfill('0');
std::cout << std::setw(5) << 42 << '\n';
std::cout << std::setw(5) << 7 << '\n';
return 0;
} How It Works setfill('0') chooses 0 as the padding character instead of a space.right puts the padding in front of the number, so 42 becomes 00042.The width is set again for the second line. 7 gets four zeros. Example 3 — Name on the Left, Number on the Right Mix left and right to make a tidy two-column list.
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::left << std::setw(8) << "Pen"
<< std::right << std::setw(6) << 12 << '\n';
std::cout << std::left << std::setw(8) << "Notebook"
<< std::right << std::setw(6) << 150 << '\n';
return 0;
} How It Works The name is left-aligned in eight places and the number is right-aligned in six places. The numbers end in the same column, so 12 and 150 line up on their last digit. left and right are switched on every line, because each of them stays on until you change it.Frequently Asked Questions What does right do in C++? It aligns padded output to the right of its field, so padding spaces are added in front of the value.
Is right the default alignment? Yes. Without any manipulator, values are right-aligned inside the width from setw.
Why use right if it is the default? To switch back after left, and to make your code easy to read.
How do I pad numbers with zeros? Use std::right, std::setfill('0'), and std::setw(n). For example 42 with width 5 prints 00042.
Does right work without setw? No visible effect. There must be a field width to create padding.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion