dc (abbreviated from desk calculator) is a cross-platform reverse-Polish calculator program that supports arbitrary-precision arithmetic. It is one of the oldest Unix utilities, predating even the development of the C programming language. The program was written by Lorinda Cherry and Robert Morris at Bell Labs.
History
dc is considered the oldest surviving Unix language program. When Bell Labs received a PDP-11, dc—written in the B programming language—was the first program to run on the new computer, before even an assembler was available. Ken Thompson has suggested that dc was the very first program written on the machine. It is available on Unix, Unix-like operating systems, and Plan 9.
Operation
dc operates on a stack-based paradigm using Reverse Polish Notation (RPN). Numbers are pushed onto a stack, and operators pop values from the stack, perform operations, and push results back. For example, the expression 4 5 * p pushes 4 and 5 onto the stack, multiplies them, and prints the result (20).
The arithmetic precision is controlled with the k command, which sets the number of fractional digits used in operations. The default precision is zero; setting a higher precision with k enables arbitrary numbers of decimal places.
Features
dc supports a range of operations and features:
- Arithmetic and stack operations: Includes standard arithmetic, square root (
v), stack manipulation (swap withr, duplicate withd). - Input/output: Reads from standard input or files; the
pcommand prints the top of stack,npops and prints without a trailing newline, andfprints the entire stack. It supports arbitrary input and output radices (e.g., hex-to-binary conversion). - Registers: Named storage locations (single character names) allowing values to be stored and retrieved, with support for secondary stacks.
- Strings and macros: Strings enclosed in brackets can be stored in registers and executed as commands, enabling macro programming.
- Conditionals: Commands such as
=r,>,<,!=execute stored macros based on comparisons of stack values.
Relationship to bc
The bc calculator program (which uses infix notation) was traditionally implemented on top of dc. However, the modern GNU implementation of dc is instead based on bc. Brian Kernighan and Ken Thompson have discussed dc's historical role in the development of Unix.
Applications
Beyond simple calculation, dc has been used for more sophisticated tasks, including unit conversion, computing greatest common divisors, factorials, prime number generation, integer factorization, calculating digits of pi, and performing Diffie–Hellman key exchange (sometimes embedded within short Perl scripts).
Related concepts
- bc (programming language)
- Calculator input methods
- Reverse Polish notation
- Stack machine
References
- dc(1) — an arbitrary precision calculator, Linux user commands manual
- McIlroy, M. D. (1987). "A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986" (Technical report). CSTR. Bell Labs. 139.
- Ritchie, Dennis M. (Sep 1979). "The Evolution of the Unix Timesharing System".