C++ oct Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, oct sets the number base of a stream to 8 (octal). Integers are then printed with the digits 0 to 7 only.

Octal is not common today, but you still see it in Unix file permissions such as 755. The setting works for output and input and stays on until changed with dec or hex. It comes from <ios>, which <iostream> includes.

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::oct into the stream before the numbers.

C++
std::cout << std::oct << 64;       // 100
std::cin >> std::oct >> n;         // reads "17" as 15
CallWhat it does
std::cout << std::oct << 64Prints 100.
std::cout << std::oct << std::showbase << 8Prints 010.
std::cin >> std::oct >> nReads an octal number such as 17.

Digits 8 and 9 do not exist in octal. Reading 19 with oct stops after the 1.

Library declarations
C++
ios_base& oct(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << oct. 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 oct.

C++ Examples

Example 1 — Print Numbers in Octal

See how some values look in base 8.

C++
#include <iostream>

int main() {
    std::cout << std::oct;
    std::cout << 8 << '\n';
    std::cout << 9 << '\n';
    std::cout << 64 << '\n';
    return 0;
}

How It Works

  1. In octal there are only eight digits, so after 7 the next number is 10. That is why 8 prints as 10.
  2. 9 is one eight and one more, so it prints as 11.
  3. 64 is 8 * 8, which is 100 in octal.

Example 2 — Show a Unix Permission Number

The decimal value 493 is the same number as the octal permission 755.

C++
#include <iostream>

int main() {
    int mode = 493;

    std::cout << "decimal: " << mode << '\n';
    std::cout << "octal:   " << std::oct << mode << '\n';
    return 0;
}

How It Works

  1. mode holds the value 493. The first line prints it in the normal way.
  2. std::oct switches the stream to base 8, and 493 is 7 * 64 + 5 * 8 + 5.
  3. The second line shows 755, the form people use for file permissions.

Example 3 — Read an Octal Number

With oct on the input stream, the text 17 means fifteen.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("17");
    int n = 0;

    in >> std::oct >> n;

    std::cout << "value: " << n << '\n';
    return 0;
}

How It Works

  1. std::oct makes >> treat the digits as base 8.
  2. 17 in base 8 is 1 * 8 + 7, which is 15.
  3. cout is still in decimal mode, so it prints the value as 15.

Frequently Asked Questions

It sets the number base of a stream to 8, so integers are printed and read as octal numbers.
Octal has only the digits 0 to 7. After 7 comes 10, which means one eight and zero ones.
Mostly for Unix file permissions like 755 or 644.
Add std::showbase. With oct it prints a leading 0, for example 010.
Put std::dec into the stream.

Next: print()

Print formatted text without a new line (C++23).

print() 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.

15 people found this page helpful