Skip to content
systemcoding.dev
Fundamentals

Octal Explained and Where You Still See It in Unix Permissions

Learn what octal is, how three bits map to one octal digit, and why chmod 755 and the C leading zero still use base 8 on Unix systems today.

5 min read
Summarize in AI
Flat illustration of a hex dump with one byte highlighted, with the C, Python and Zig logos.

Octal is the number system with base eight, using the digits 0 to 7. It once dominated computing, and today it survives in two places that every Unix user meets: file permission modes such as chmod 755, and the C rule that a number starting with a zero is read as octal. This post explains the digits, why three bits make one octal digit, how permissions are encoded, and the leading-zero trap in C that has produced real bugs. The command-line examples target GNU coreutils 9.5 and a POSIX shell, and the C examples target GCC 14.

What octal is

In octal, each place is worth eight times the place to its right, so the weights from the right are 1, 8, 64, 512. Only the digits 0 to 7 exist, since reaching eight rolls the place over: octal 10 is eight in decimal. The number 755 in octal is 7 times 64, plus 5 times 8, plus 5, which is 448 plus 40 plus 5, giving 493 in decimal.

The useful property is the same one that makes hexadecimal useful. Eight is 2 to the power 3, so one octal digit holds exactly three bits. Converting between octal and binary is a lookup with no arithmetic.

OctalBinaryOctalBinary
00004100
10015101
20106110
30117111

To read the octal number 644 as bits, expand each digit: 110 100 100. To write the bit pattern 111 101 101 in octal, replace each group of three: 755.

Why octal fell out of favour

Early machines such as the PDP-8 had 12-bit words and others had 36-bit words, and both divide cleanly into groups of three bits, so octal was the natural shorthand. When the eight-bit byte became the standard unit, it stopped fitting: eight bits divide into two groups of four, and a group of three leaves two bits over. Hexadecimal fits a byte exactly, so it took over for dumps, addresses and registers. Octal stayed alive wherever three-bit fields are the natural structure, and the standout is Unix permissions.

The permission bits

A file on a Unix system carries a mode that says who may read, write or execute it. The mode has three classes of user, and each class has three permission bits.

ClassMeaning
user (u)the file's owner
group (g)members of the file's group
other (o)everyone else
BitWeightMeaning on a file
r4read the contents
w2modify the contents
x1execute the file as a program

Each class holds three bits, so each class is one octal digit, built by adding the weights of the permissions granted. Read plus write is 4 plus 2, which is 6. Read plus execute is 4 plus 1, which is 5. All three is 7. The full mode is three such digits, for user, group and other in that order.

That is why chmod 755 is common. It reads as 7 5 5: the owner has read, write and execute, and the group and everyone else have read and execute. The mode 644 gives the owner read and write and everyone else read only, which is the usual choice for a plain data file. The mode 600 keeps a private key readable only by its owner.

The -rwxr-xr-x string that ls -l prints is the same three digits spelled out as letters.

Shell
touch notes.txt
chmod 640 notes.txt
ls -l notes.txt
stat -c '%a %A %n' notes.txt
Text
-rw-r----- 1 alice staff 0 Nov 23 10:00 notes.txt
640 -rw-r----- notes.txt

The user and group names, the size and the timestamp will differ on your machine. The mode part is fixed: 640 is 110 100 000, which is read and write for the owner, read for the group and nothing for others, and ls renders that as rw-, r--, ---. The %a format of GNU stat prints the octal mode, and %A prints the letter form.

There are also three special bits that sit in a fourth octal digit in front: setuid (4), setgid (2) and the sticky bit (1). A mode of 1777 on a directory such as /tmp means everyone can create files, and only the owner of a file can delete it. The leading digit is usually omitted when it is zero.

Octal in C

The C language inherited the octal convention from its Unix origins. A constant that begins with a zero is octal. The system call that sets a mode on creation takes such a constant, and it is where you see octal in real C code.

mode.cc
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
    int fd = open("out.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0) {
        perror("open");
        return 1;
    }
    printf("mode argument as decimal: %d\n", 0644);
    close(fd);
    return 0;
}

The third argument 0644 is an octal constant, and the kernel combines it with the process umask when it creates the file. The decimal printout shows what the compiler really stored.

Text
mode argument as decimal: 420

Octal 644 is 6 times 64, plus 4 times 8, plus 4, which is 384 plus 32 plus 4, giving 420. If you wrote 644 without the leading zero, the compiler would read decimal 644, which is octal 1204, a mode with the sticky bit and odd permissions. The compiler would not complain, since both are valid integers. Using the named constants such as S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH from sys/stat.h avoids the problem at the cost of verbosity.

The leading zero trap

The same rule bites in the other direction. A programmer who pads a number with leading zeros for alignment gets octal without warning:

zero_pad.cc
#include <stdio.h>

int main(void)
{
    int a = 010;
    int b = 017;
    printf("%d %d\n", a, b);
    return 0;
}
Text
8 15

010 is octal one-zero, which is eight, and 017 is fifteen. A table of values written as 001, 002, ... 008 would not even compile at the 008 entry, because 8 is not an octal digit, and GCC reports invalid digit "8" in octal constant. Modern style guides avoid decimal constants with leading zeros for this reason. Standard C, including C23, has no 0o prefix, but Rust and Zig write octal as 0o17, which makes the intent explicit, and the post on literals compares the three languages.

Octal escapes in strings

A backslash followed by up to three octal digits inside a string or character constant is an octal escape, so a backslash and 101 spell the character with code 65, which is A. Tools that print unprintable bytes use the same form for the escape character, where 033 is octal 27. Three octal digits cover values up to 511 and a byte needs only up to 255, so the first digit is 0 to 3.

Converting quickly

Octal to decimal is the place-weight sum shown above. Decimal to octal uses repeated division by eight. For 493, divide by 8 to get 61 remainder 5, then 61 by 8 gives 7 remainder 5, then 7 by 8 gives 0 remainder 7, so the digits from last to first are 755. When you start from bits, group them in threes from the right and look each group up.

Taggedcbeginner
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.