Undated photograph, probably circa late nineteen sixties of a Unix developers Ken Thompson and Dennis Ritchie at a DEC PDP 11 computer running Unix. In the left of the photo, Ritchie is standing and watching Thompson, who is sitting at one of two teletype terminals used as consoles to interface with the computer.

A beginner’s guide to dc

Imagine it’s the 1970s and you’re writing a program to simulate a physical event. (This was a common use case for computers at the time, especially using the FORTRAN programming language.) Your subroutine requires using a tricky constant value, which you’ve worked out using algebra as the square root of 480 divided by 3.

You could enter that calculation into the program, and let the FORTRAN program calculate SQRT(480.)/3. each time. But calculating a square root can be slow, especially on 1970s hardware. It will be faster to enter that value as a constant in the program itself. How would you calculate the square root of 480 divided by 3 so you can enter that as a number in the program?

Old-style calculators

In 2025, you’d probably use the calculator app on your phone. Before the era of the smartphone, you’d probably use a pocket calculator, like the Texas Instruments TI-36X calculator that I used throughout my undergraduate physics program in the 1990s.

But in the 1970s, the “pocket calculator” was an expensive device; the Hewlett Packard HP-35 calculator was introduced in the early 1970s and cost $395 (that’s over $3000 in today’s dollars, which is quite an investment). Pocket calculators became less expensive over time, such as the mid to late 1970s, although less expensive scientific pocket calculators became available later, around the late 1970s to early 1980s.

Instead, a programmer in the 1970s would likely use a desktop calculator to calculate the square root of 480 divided by 3. A desktop calculator was a big machine that sat on your desk (hence its name) and was dedicated to performing all kinds of calculations. These were still expensive, but less so than a pocket scientific calculator.

Calculators at the time typically used Reverse Polish Notation (RPN) where you entered numbers separately, then pressed a key to perform an operation. Entering values was done on a stack, which was “first in, last out.” That is, adding two numbers like 2+4 required entering the value 2 into the stack (called “pushing”), then entering the value 4 into the stack, then pressing the “+” key. This took the two most recent values (2 and 4) off the stack (called “popping”) and added them, then pushed the answer (6) back onto the stack.

The desktop calculator: dc

In the early 1970s, one of the first programs written for the PDP-11 at Bell Labs was dc, the “desktop calculator” program created by Lorinda Cherry and Robert Morris. It is also one of the oldest original Unix programs, and it’s still available on Linux today. I mostly use dc to perform quick calculations while I’m at the command line.

Not surprisingly, dc uses RPN like every other calculator from the 1970s. This may seem strange if you aren’t used to RPN, but it gets easier after you’ve used it for a while. The key to using RPN in dc is to enter each number and operation, surrounded by whitespace like a space, tab, or new line. For example, to calculate 2+4, you push the values 2 and 4 onto the stack, then enter “+” to add them. For example:

$ dc
2
4
+

This pops 2 and 4 off the stack, adds them, and pushes 6 back onto the stack. But you’ll notice that dc doesn’t print the result. That’s because when dc was created, Bell Labs used the TeleType terminal, which printed everything on paper (not a screen). And printing on a TeleType is loud and slow. If dc didn’t need to print something, it didn’t. The user might have more calculations to do with this value, so it doesn’t print the result until you use the “p” command. Let’s add the “p” command to the session, but this time I’ll enter the values and operation on the first line, to show you can use any whitespace to separate the values:

$ dc
2 4 +
p
6

If that’s the only calculation you need to do, you can quit the program by entering the “q” command, or pressing ctrl+D on the keyboard.

Sample calculations with dc

Let’s do a few sample calculations with dc to see how it works. Remember, to see the results, you need to add the “p” command, which I’ll include.

We’ve already seen an example of addition, so let’s start with subtraction. To subtract 7 from 72, you push 72 and 7 onto the stack, in that order, then use the “-” command to subtract them.

72 7 -
p
65

Printing a value with “p” doesn’t change the stack, which now contains only the value 65. If we needed to subtract another number from this value, such as 65-12, we can push the number 12 onto the stack, then use the “-” command to subtract 12 from 65:

12 -
p
53

If we needed to subtract two different numbers, such as 22-9, we can enter those two values into the stack and use the “-” command again. The stack will actually have three values in it (53, 22, and 9) but the basic arithmetic commands (“+”, “-“, “*” and “/”) only operate on the two most recent entries:

22 9 -
p
13

Values on the stack

After the “-” command, the stack now has two values in it: 53 (left over from the previous calculation) and 13 (from this calculation). If you want to see the contents of the stack, use the “f” command:

f
13
53

These values are actually shown in reverse order, which we can see if we use “-” one more time. This will subtract 13 from 53.

- p
40

We can use the “f” command one more time to show that only one value is left in the stack after subtracting these two values:

f
40

Adjusting the precision

Let’s say you want to divide two values, such as 42 by 6. You use the “/” command to divide the two most recent entries in the stack. I’ll start this session with the “c” command to clear the stack, and “f” to show that there’s nothing in there, then I’ll enter and print the division calculation:

c
f
42 6 /
p
7

This result is as you would expect, because 6 times 7 really is the value 42. The division is even, without any remainder. But what happens when you divide two numbers that don’t produce a nice value like that? Let’s try it by dividing 1 by 4, which should give 0.25:

c
1 4 /
p
0

Oh no! That gives a zero value. This is because dc is an arbitrary precision calculator, and you can adjust the precision to any number of decimal places. By default, the precision is zero, which works only for integer values. To set the precision to some other value, like 5 decimal places, enter the number 5 into the stack and use the “k” command. If you repeat the calculation, you’ll get the expected result:

5 k
1 4 /
p
.25000

Calculating with dc

To get started with dc, I recommend you stick to the basics. These basic operations are enough to solve the calculations that I need to do on a day-to-day basis:

operationwhat it does
+Addition
-Subtraction
*Multiplication
/Division
%Modulo (remainder after division) such as 13 4 % to calculate 1
^Exponent, such as 4 2 ^ to calculate 4 squared
vSquare root (you might also need to set the precision)

Use these commands to interact with the calculator, such as to clear the stack or manipulate values. Again, as you get started with dc, keep it to the basics:

commandwhat it does
cclear the stack
dduplicate the value on the stack, such as 3 d * to calculate 3 squared
rreverse the two “top” entries on the stack
kset the precision (number of decimal places)
qquit

So let’s go back to the original problem: calculate the square root of 480 divided by 3. That is not a round number, so we should first set the precision with the “k” command. Calculate the square root of 480, divide that result by 3, then print the result. I’ll group the calculations and commands on separate lines, but these could all be entered on separate lines or all on one line:

$ dc
8 k
480 v
3 /
p
7.30296743

Let’s verify that result: if I enter that calculation into a calculator app on my phone, the square root of 480 is about 21.9089023. And 21.9089023 divided by 3 is 7.3029674334.

The dc command is a handy tool to use when you are working at the command line and need to calculate a value quickly. The calculator app on my phone, or the calculator app on my desktop system, is convenient most of the time—but if I’m working at the command line and just need to calculate something, pulling up a separate app is a distraction. Using dc keeps me at the command line, where I’m already doing my work.

I’ve only covered the basics of dc here. If you want to do more with it, use man dc to explore the manual page.

Leave a Reply