C++ right Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iomanip

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.

C++
#include <iomanip>

std::cout << std::right << std::setw(8) << "ab";   //       ab
CallWhat 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
C++
ios_base& right(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << right. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after right.

C++ Examples

Example 1 — Go Back to Right Alignment

Use left first, then right to return.

C++
#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

  1. left puts the padding after the text, so ab stays at the left edge.
  2. right turns that off and puts the padding in front of the text.
  3. 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.

C++
#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

  1. setfill('0') chooses 0 as the padding character instead of a space.
  2. right puts the padding in front of the number, so 42 becomes 00042.
  3. 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.

C++
#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

  1. The name is left-aligned in eight places and the number is right-aligned in six places.
  2. The numbers end in the same column, so 12 and 150 line up on their last digit.
  3. left and right are switched on every line, because each of them stays on until you change it.

Frequently Asked Questions

It aligns padded output to the right of its field, so padding spaces are added in front of the value.
Yes. Without any manipulator, values are right-aligned inside the width from setw.
To switch back after left, and to make your code easy to read.
Use std::right, std::setfill('0'), and std::setw(n). For example 42 with width 5 prints 00042.
No visible effect. There must be a field width to create padding.

Next: scientific

Print floating-point numbers in scientific notation.

scientific tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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