dc, an arbitrary precision calculator

by Ken Pizzini
original manual by Richard Stallman


Table of Contents


1. Introduction

DC is a reverse-polish desk calculator which supports unlimited precision arithmetic. It also allows you to define and call macros. Normally DC reads from the standard input; if any command arguments are given to it, they are filenames, and DC reads and executes the contents of the files instead of reading from standard input. All normal output is to standard output; all error messages are written to standard error.

To exit, use `q'. C-c does not exit; it is used to abort macros that are looping, etc. (Currently this is not true; C-c does exit.)

A reverse-polish calculator stores numbers on a stack. Entering a number pushes it on the stack. Arithmetic operations pop arguments off the stack and push the results.

To enter a number in DC, type the digits, with an optional decimal point. Exponential notation is not supported. To enter a negative number, begin the number with `_'. `-' cannot be used for this, as it is a binary operator for subtraction instead. To enter two numbers in succession, separate them with spaces or newlines. These have no meaning as commands.

2. Invocation

DC may be invoked with the following command-line options:

`-e expr'
`--expression=expr'
Evaluate expr as DC commands.
`-f file'
`--file=file'
Read and evaluate DC commands from file.
`-h'
`--help'
Print a usage message summarizing the command-line options, then exit.
`-V'
`--version'
Print the version information for this program, then exit.

If any command-line parameters remain after processing the options, these parameters are interpreted as additional files whose contents are read and evaluated. A file name of - refers to the standard input stream. If no -e option was specified, and no files were specified, then the standard input will be read for commands to evaluate.

3. Printing Commands

`p'
Prints the value on the top of the stack, without altering the stack. A newline is printed after the value.
`n'
Prints the value on the top of the stack, popping it off, and does not print a newline after. (This command is a GNU extension.)
`P'
Pops off the value on top of the stack. If it it a string, it is simply printed without a trailing newline. Otherwise it is a number, and the integer portion of its absolute value is printed out as a "base (UCHAR_MAX+1)" byte stream. Assuming that (UCHAR_MAX+1) is 256 (as it is on most machines with 8-bit bytes), the sequence KSK 0k1/ [_1*]sx d0>x [256~aPd0<x]dsxx sxLKk cou