We ❤️ Open Source
A community education resource
Getting started with dc: The command-line calculator hiding in your terminal
Explore the original desktop calculator that does more than add.

This article is part of the eBook: Learning to program: A starter guide to stacks and command-line calculators, a free download from We Love Open Source.
You may have heard that programmers are strongly divided on “spaces versus tabs,” but others are also equally divided on “how to enter numbers into a calculator.” If you’re familiar with entering 1 + 3 * 4 in a calculator app to get 13, then you’re using algebraic notation, also called infix notation. With this notation, you enter the numbers into the calculator like a normal human would read them, and rely on the calculator to know how to work out the answer: multiplication and division first, then addition and subtraction. So 3 * 4 is 12, and 1 + 12 is 13.
But earlier calculators didn’t have enough memory or computing power to manage algebraic notation—or at least, to do it cost effectively. These calculators used a model called Reverse Polish Notation, also called postfix notation. With this method, you enter the numbers into the calculator, then call an operation to do something with those values. For example, the * operation takes the two most recent values entered into the calculator, multiplies them, and puts the answer back into memory. Or, the + operation takes the two most recent values, adds them, and stores the answer in memory.
Read more: 10 open source tools you can start using today
To calculate 1 + 3 * 4 using Reverse Polish Notation, you enter the values 1, 3, and 4 into memory, then use the * action to multiply 3 and 4. That also puts 12 back into memory, so when you use the + action, the calculator adds 1 and 12 to get 13.
Another way to describe that is: Enter (or “push”) the values 1, 3, and 4 into the memory stack. Then the stack is:
4
3
1
Because it’s a stack, the first item is at the bottom of the stack, and the most recently stored item is at the top of the stack. Use the * action to retrieve (or “pop”) the two most recent values (3 and 4), multiply them, and put (or “push”) the answer back into memory. The stack is now:
12
1
Use the + action to “pop” the two values from memory, add them, and “push” the answer back onto the stack. The stack is now just a single value: 13.
A desktop calculator
The dc command is a desktop calculator, and is one of the oldest Unix commands. Written in the early 1970s by Lorinda Cherry and Robert Morris at Bell Labs, dc uses Reverse Polish Notation like other calculators of the era. It’s also an adjustable precision calculator that supports a macro language, which makes it both powerful and flexible. The dc command doesn’t display a prompt, so you might assume the program has hung—but really, it’s just waiting for you to type something.
When entering values into dc, you need to separate them with whitespace (tabs, spaces, and newlines) but operations don’t always need spaces. But let’s keep things simple and use whitespace around everything.
Let’s explore dc with a few basic operations. The + (addition) and * (multiplication) operations work the way you might assume: Take the two most recent numbers and either add them or multiply them; the order doesn’t matter. Using – (subtraction) and / (division) requires some attention to how the values were entered. To subtract 4 – 3, enter 4 then 3, so the stack looks like this:
3
4
When you use the – operation, dc pops off the two values; the result is the second value popped (the first value entered) minus the first value popped (the second value entered). The answer will be 1, which will then be the only value left in the stack.
The dc calculator doesn’t print the result of each calculation; to see the most recent value in the stack (called “peek”) you use the p command. Here’s a complete dc session to subtract 3 from 4 and print the result:
$ dc
4 3 -
p
1
Use the q command to quit, or press ctrl+d on your keyboard. Either will exit the dc program and return you to the shell.
Division is similar to subtraction, but you have to keep in mind that dc treats values as integers by default. To calculate floating point values, with numbers after the decimal point, you need to adjust the precision. That means if you want to divide 1 by 4, you need to adjust the precision to 2, so you can see two numbers after the decimal point.
Use the k instruction to set the precision; this pops the most recently entered value for the precision. To divide 1 by 4, you first enter 2 k, then do the division:
$ dc
2 k
1 4 /
p
.25
Read more: How to write your first FreeDOS program
More to explore
The dc calculator supports a ton of other commands and actions, more than just the list of basic operations I’ve presented here. Use man dc to see everything you can do. I find the most useful math operations include:
| Operator | Action |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulo (remainder after division) |
| ^ | Exponent |
| v | Square root |
You can also use these commands to modify the stack or do other things in the calculator:
| Command | Action |
|---|---|
| k | Set the precision |
| d | Duplicate the value on the stack |
| r | Reverse the top two values on the stack |
| c | Clear the stack |
| p | Print the top value in the stack |
| f | Print the full list of the stack |
| q | Quit |
Download the free starter guide to command-line stacks
The opinions expressed on this website are those of each author, not of the author's employer or All Things Open/We Love Open Source.