dc (computer program)

[1] It was written by Lorinda Cherry and Robert Morris at Bell Labs.

[5] This article provides some examples in an attempt to give a general flavour of the language; for a complete list of commands and syntax, one should consult the man page for one's specific implementation.

When its home Bell Labs received a PDP-11, dc—written in B—was the first language to run on the new computer, even before an assembler.

This evaluates the line as if it were a dc command, and so it is necessary that it be syntactically correct and presents a potential security problem because the !

Hex digits must be in upper case to avoid collisions with dc commands and are limited to A-F.

As an example, to convert from hex to binary: In addition to these basic arithmetic and stack operations, dc includes support for macros, conditionals and storing of results for later retrieval.

String values are enclosed in [ and ] characters and may be pushed onto the stack and stored in registers.

Macros are then implemented by allowing registers and stack entries to be strings as well as numbers.

The command =r pops two values from the stack, and executes the macro stored in register r only if they are equal.

A simple factorial of the top of the stack might be implemented as: The 1Q command exits from a macro, allowing an early return.

This is implemented with a macro stored in register a which conditionally calls itself, performing an addition each time, until only one value remains on the stack.

This is again implemented with a macro stored in register a which conditionally calls itself, performing an addition each time, until only one value remains on the stack.

Note that since dc supports arbitrary precision, there is no concern about numeric overflow or loss of precision, no matter how many lines the input stream contains, unlike a similarly concise solution in AWK.

Downsides of this solution are: the loop stops on encountering a blank line in the input stream (technically, any input line which does not add at least one numeric value to the stack); and, for handling negative numbers, leading instances of '-' to denote a negative sign must be change to '_' in the input stream, because of dc's nonstandard negative sign.

As an example of a relatively simple program in dc, this command (in 1 line): converts distances from metres to feet and inches; the bulk of it is concerned with prompting for input, printing output in a suitable format and looping around to convert another number.

As an example, here is an implementation of the Euclidean algorithm to find the GCD: Computing the factorial of an input value,

But as pi is a transcendental number, the program will continue until interrupted or resource exhaustion of the machine it is run on.

A fast divide and conquer implementation of the same formula that doubles in size each iteration.

A more complex example of dc use embedded in a Perl script performs a Diffie–Hellman key exchange.

This was popular as a signature block among cypherpunks during the ITAR debates, where the short script could be run with only Perl and dc, ubiquitous programs on Unix-like operating systems:[8] A commented version is slightly easier to understand and shows how to use loops, conditionals, and the q command to return from a macro.

With the GNU version of dc, the | command can be used to do arbitrary precision modular exponentiation without needing to write the X function.