# man > dc(1)

---
type: CommandReference
command: dc
mode: man
section: 1
source: man-pages
---

## Quick Reference

- `dc` — Start an interactive session
- `dc path/to/script.dc` — Execute a script
- `dc -e '10 k 5 3 / p'` — Calculate expression with scale 10
- `dc -e '4 5 * 17 - p'` — Calculate 4*5 - 17
- `dc -e '7 k 5 _3 / p'` — Calculate 5 / -3 with precision 7
- `dc -e '100 k 5 v 1 + 2 / p'` — Calculate golden ratio (phi)

## Name

dc - an arbitrary precision calculator

## Synopsis

`dc [-V] [--version] [-h] [--help] [-e scriptexpression] [--expression=scriptexpression] [-f scriptfile] [--file=scriptfile] [file ...]`

## Options

### General Options

- `-V`, `--version` — Print version and copyright, then exit
- `-h`, `--help` — Print usage summary and bug-reporting address, then exit
- `-e`, `--expression=script` — Add commands from script to the execution set
- `-f`, `--file=script-file` — Add commands from script-file to the execution set

If any arguments remain after processing the above, they are interpreted as input filenames. A filename of `-` refers to standard input. Standard input is processed if no script files or expressions are specified.

### Printing Commands

- `p` — Print top of stack (value remains on stack), with newline
- `n` — Print top of stack, pop it, no newline
- `P` — Pop top of stack. If string, print without newline. If number, print integer portion as base (UCHAR_MAX+1) byte stream (typically 256)
- `f` — Print entire stack without altering anything

### Arithmetic

- `+` — Pop two values, add, push result (exact precision)
- `-` — Pop two values, subtract first popped from second, push result
- `*` — Pop two values, multiply, push result (fraction digits depend on precision and arguments)
- `/` — Pop two values, divide second popped by first, push result (fraction digits from precision)
- `%` — Pop two values, compute remainder of division (as if `/` then `*-`)
- `~` — Pop two values, divide second by first, push quotient then remainder
- `^` — Pop two values (exponent first, base second), exponentiate (fraction part of exponent ignored), result precision from precision value
- `|` — Pop three values (modulus, exponent, base), compute modular exponentiation (modulus non-zero, exponent non-negative, base integer)
- `v` — Pop one value, compute square root, push result (precision = max(precision, argument precision))

Most arithmetic operations are affected by the precision set with `k` (default 0, giving integer results except + and -).

### Stack Control

- `c` — Clear stack
- `d` — Duplicate top of stack
- `r` — Swap top two values
- `R` — Pop top-of-stack as integer n, cyclically rotate top n items. Positive n: top becomes second-from-top. Negative n: top becomes n-th from top. If stack depth < n, entire stack rotates.

### Registers

- `s r` — Pop value and store in register r (single character)
- `l r` — Push value from register r (0 if uninitialized), does not alter register
- `S r` — Pop value from main stack and push onto register r's stack
- `L r` — Pop value from register r's stack and push onto main stack

Each register has its own stack. The current register value is the top of that stack.

### Parameters

- `i` — Pop value, set input radix (2-16)
- `o` — Pop value, set output radix (>=2)
- `k` — Pop value, set precision (>=0, in decimal digits)
- `I` — Push current input radix
- `O` — Push current output radix
- `K` — Push current precision

### Strings

- `[characters]` — Push string containing characters (balanced brackets)
- `a` — Pop top-of-stack. If number, push low-order byte as string. If string, push first character back
- `x` — Pop value, execute as macro (if string). If number, push back
- `> r` — Pop two values, execute register r as macro if top-of-stack > second-to-top
- `!> r` — Execute macro if top-of-stack <= second-to-top
- `< r` — Execute macro if top-of-stack < second-to-top
- `!< r` — Execute macro if top-of-stack >= second-to-top
- `= r` — Execute macro if two popped values are equal
- `!= r` — Execute macro if two popped values are not equal
- `?` — Read a line from terminal and execute it
- `q` — Exit from macro and its caller (if top-level, exit dc)
- `Q` — Pop value, exit that many levels of macro execution (never exits dc)

### Status Inquiry

- `Z` — Pop value, push number of decimal digits (or characters if string)
- `X` — Pop value, push number of fraction digits (0 for string)
- `z` — Push current stack depth before execution of `z`

### Miscellaneous

- `!` — Execute rest of line as system command (note: `!<`, `!=`, `!>` take precedence; add space after `!` to avoid)
- `#` — Comment (rest of line ignored)
- `: r` — Pop top two values, store second-to-top in array r indexed by top-of-stack
- `; r` — Pop top-of-stack, use as index into array r, push selected value

Each stacked instance of a register has its own array.

## Examples

shell
# Interactive session (no arguments)
dc
4 5 * 17 - p   # prints 3
shell
# Calculate with scale
dc -e '10 k 5 3 / p'   # prints 1.6666666666
shell
# Negative numbers (use underscore)
dc -e '7 k 5 _3 / p'   # prints -1.6666666
shell
# Square root and golden ratio
dc -e '100 k 5 v 1 + 2 / p'   # prints 1.6180339887...
shell
# Macro example
echo '[1p]sa 1 2>a' | dc   # prints 1 (since 2 > 1)
shell
# Array example
echo '1 0:a 0Sa 2 0:a La 0;ap' | dc   # prints 1
## See Also

- [dc(1) man page](https://www.chedong.com/phpMan.php/man/dc/1/markdown)