Home C++ Iostream Functions oct C++ oct Manipulator 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.
std::cout << std::oct << 64; // 100
std::cin >> std::oct >> n; // reads "17" as 15 Call What 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 ios_base& oct(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << oct. 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 oct.
Important
oct stays on for all later integers, so go back with std::dec when you are done. In C++ source code a number that starts with 0, such as 010, is octal too, which is a common reason for surprising results.
C++ Examples 1. Print numbers 2. File permissions 3. Read octal Example 1 — Print Numbers in Octal See how some values look in base 8.
#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 In octal there are only eight digits, so after 7 the next number is 10. That is why 8 prints as 10. 9 is one eight and one more, so it prints as 11.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.
#include <iostream>
int main() {
int mode = 493;
std::cout << "decimal: " << mode << '\n';
std::cout << "octal: " << std::oct << mode << '\n';
return 0;
} How It Works mode holds the value 493. The first line prints it in the normal way.std::oct switches the stream to base 8, and 493 is 7 * 64 + 5 * 8 + 5.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.
#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 std::oct makes >> treat the digits as base 8.17 in base 8 is 1 * 8 + 7, which is 15.cout is still in decimal mode, so it prints the value as 15.Frequently Asked Questions What does oct do in C++? It sets the number base of a stream to 8, so integers are printed and read as octal numbers.
Why is 8 printed as 10 with oct? Octal has only the digits 0 to 7. After 7 comes 10, which means one eight and zero ones.
Where is octal used today? Mostly for Unix file permissions like 755 or 644.
How do I show the leading zero? Add std::showbase. With oct it prints a leading 0, for example 010.
How do I go back to decimal? Put std::dec into the stream.
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.
15 people found this page helpful
Helpful Share Copy link Suggestion