<s>
dc	B-Application
(	O
desk	O
calculator	O
)	O
is	O
a	O
cross-platform	B-Operating_System
reverse-Polish	O
calculator	O
which	O
supports	O
arbitrary-precision	B-Algorithm
arithmetic	I-Algorithm
.	O
</s>
<s>
Written	O
by	O
Lorinda	O
Cherry	O
and	O
Robert	O
Morris	O
at	O
Bell	O
Labs	O
,	O
it	O
is	O
one	O
of	O
the	O
oldest	O
Unix	B-Application
utilities	O
,	O
preceding	O
even	O
the	O
invention	O
of	O
the	O
C	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
Traditionally	O
,	O
the	O
bc	B-Application
calculator	O
program	O
(	O
with	O
infix	O
notation	O
)	O
was	O
implemented	O
on	O
top	O
of	O
dc	B-Application
.	O
</s>
<s>
This	O
article	O
provides	O
some	O
examples	O
in	O
an	O
attempt	O
to	O
give	O
a	O
general	O
flavour	O
of	O
the	O
language	O
;	O
for	O
a	O
complete	O
list	O
of	O
commands	O
and	O
syntax	O
,	O
one	O
should	O
consult	O
the	O
man	B-Protocol
page	I-Protocol
for	O
one	O
's	O
specific	O
implementation	O
.	O
</s>
<s>
dc	B-Application
is	O
the	O
oldest	O
surviving	O
Unix	B-Application
language	O
program	O
.	O
</s>
<s>
When	O
its	O
home	O
Bell	O
Labs	O
received	O
a	O
PDP-11	B-Device
,	O
dcwritten	O
in	O
Bwas	O
the	O
first	O
language	O
to	O
run	O
on	O
the	O
new	O
computer	O
,	O
even	O
before	O
an	O
assembler	O
.	O
</s>
<s>
Ken	O
Thompson	O
has	O
opined	O
that	O
dc	B-Application
was	O
the	O
very	O
first	O
program	O
written	O
on	O
the	O
machine	O
.	O
</s>
<s>
To	O
multiply	O
four	O
and	O
five	O
in	O
dc	B-Application
(	O
note	O
that	O
most	O
of	O
the	O
whitespace	O
is	O
optional	O
)	O
:	O
</s>
<s>
Then	O
the	O
p	O
command	B-Application
is	O
used	O
to	O
examine	O
(	O
print	O
out	O
to	O
the	O
screen	O
)	O
the	O
top	O
element	O
on	O
the	O
stack	O
.	O
</s>
<s>
The	O
q	O
command	B-Application
quits	O
the	O
invoked	O
instance	O
of	O
dc	B-Application
.	O
</s>
<s>
The	O
arithmetic	B-Architecture
precision	I-Architecture
is	O
changed	O
with	O
the	O
command	B-Application
k	O
,	O
which	O
sets	O
the	O
number	O
of	O
fractional	O
digits	O
(	O
the	O
number	O
of	O
digits	O
following	O
the	O
point	B-Algorithm
)	O
to	O
be	O
used	O
for	O
arithmetic	O
operations	O
.	O
</s>
<s>
Since	O
the	O
default	O
precision	B-Architecture
is	O
zero	O
,	O
this	O
sequence	O
of	O
commands	O
produces	O
0	O
as	O
a	O
result	O
:	O
</s>
<s>
By	O
adjusting	O
the	O
precision	B-Architecture
with	O
k	O
,	O
an	O
arbitrary	O
number	O
of	O
decimal	B-Architecture
places	I-Architecture
can	O
be	O
produced	O
.	O
</s>
<s>
This	O
command	B-Application
sequence	O
outputs	O
.66666	O
.	O
</s>
<s>
To	O
swap	O
the	O
top	O
two	O
elements	O
of	O
the	O
stack	O
,	O
use	O
the	O
r	O
command	B-Application
.	O
</s>
<s>
To	O
duplicate	O
the	O
top	O
element	O
,	O
use	O
the	O
d	O
command	B-Application
.	O
</s>
<s>
command	B-Application
.	O
</s>
<s>
This	O
evaluates	O
the	O
line	O
as	O
if	O
it	O
were	O
a	O
dc	B-Application
command	B-Application
,	O
and	O
so	O
it	O
is	O
necessary	O
that	O
it	O
be	O
syntactically	O
correct	O
and	O
presents	O
a	O
potential	O
security	O
problem	O
because	O
the	O
!	O
</s>
<s>
dc	B-Application
command	B-Application
enables	O
arbitrary	O
command	B-Application
execution	O
.	O
</s>
<s>
dc	B-Application
also	O
supports	O
arbitrary	O
input	O
and	O
output	O
radices	O
.	O
</s>
<s>
The	O
i	O
command	B-Application
pops	O
the	O
top	O
of	O
the	O
stack	O
and	O
uses	O
it	O
for	O
the	O
input	O
base	O
.	O
</s>
<s>
Hex	O
digits	O
must	O
be	O
in	O
upper	O
case	O
to	O
avoid	O
collisions	O
with	O
dc	B-Application
commands	O
and	O
are	O
limited	O
to	O
A-F	O
.	O
</s>
<s>
The	O
o	O
command	B-Application
does	O
the	O
same	O
for	O
the	O
output	O
base	O
,	O
but	O
keep	O
in	O
mind	O
that	O
the	O
input	O
base	O
affects	O
the	O
parsing	O
of	O
every	O
numeric	O
value	O
afterwards	O
so	O
it	O
is	O
usually	O
advisable	O
to	O
set	O
the	O
output	O
base	O
first	O
.	O
</s>
<s>
To	O
read	O
the	O
values	O
,	O
the	O
K	O
,	O
I	O
and	O
O	O
commands	O
push	O
the	O
current	O
precision	B-Architecture
,	O
input	O
radix	O
and	O
output	O
radix	O
on	O
to	O
the	O
top	O
of	O
the	O
stack	O
.	O
</s>
<s>
In	O
addition	O
to	O
these	O
basic	O
arithmetic	O
and	O
stack	O
operations	O
,	O
dc	B-Application
includes	O
support	O
for	O
macros	O
,	O
conditionals	O
and	O
storing	O
of	O
results	O
for	O
later	O
retrieval	O
.	O
</s>
<s>
The	O
mechanism	O
underlying	O
macros	O
and	O
conditionals	O
is	O
the	O
register	O
,	O
which	O
in	O
dc	B-Application
is	O
a	O
storage	O
location	O
with	O
a	O
single	O
character	O
name	O
which	O
can	O
be	O
stored	O
to	O
and	O
retrieved	O
from	O
:	O
sc	O
pops	O
the	O
top	O
of	O
the	O
stack	O
and	O
stores	O
it	O
in	O
register	O
c	B-Language
,	O
and	O
lc	O
pushes	O
the	O
value	O
of	O
register	O
c	B-Language
onto	O
the	O
stack	O
.	O
</s>
<s>
The	O
a	O
command	B-Application
converts	O
the	O
low	O
order	O
byte	O
of	O
the	O
numeric	O
value	O
into	O
an	O
ASCII	O
character	O
,	O
or	O
if	O
the	O
top	O
of	O
the	O
stack	O
is	O
a	O
string	O
it	O
replaces	O
it	O
with	O
the	O
first	O
character	O
of	O
the	O
string	O
.	O
</s>
<s>
There	O
are	O
no	O
ways	O
to	O
build	O
up	O
strings	O
or	O
perform	O
string	O
manipulation	O
other	O
than	O
executing	O
it	O
with	O
the	O
x	O
command	B-Application
,	O
or	O
printing	O
it	O
with	O
the	O
P	O
command	B-Application
.	O
</s>
<s>
processed	O
as	O
a	O
sequence	O
of	O
dc	B-Application
commands	O
)	O
.	O
</s>
<s>
and	O
then	O
(	O
using	O
the	O
x	O
command	B-Application
which	O
executes	O
the	O
top	O
of	O
the	O
stack	O
)	O
we	O
can	O
use	O
it	O
like	O
this	O
:	O
</s>
<s>
The	O
command	B-Application
=	O
r	O
pops	O
two	O
values	O
from	O
the	O
stack	O
,	O
and	O
executes	O
the	O
macro	O
stored	O
in	O
register	O
r	O
only	O
if	O
they	O
are	O
equal	O
.	O
</s>
<s>
The	O
1Q	O
command	B-Application
exits	O
from	O
a	O
macro	O
,	O
allowing	O
an	O
early	O
return	O
.	O
</s>
<s>
q	O
quits	O
from	O
two	O
levels	O
of	O
macros	O
(	O
and	O
dc	B-Application
itself	O
if	O
there	O
are	O
less	O
than	O
two	O
levels	O
on	O
the	O
call	O
stack	O
)	O
.	O
</s>
<s>
A	O
bare	O
number	O
is	O
a	O
valid	O
dc	B-Application
expression	O
,	O
so	O
this	O
can	O
be	O
used	O
to	O
sum	O
a	O
file	O
where	O
each	O
line	O
contains	O
a	O
single	O
number	O
.	O
</s>
<s>
operator	O
reads	O
another	O
command	B-Application
from	O
the	O
input	O
stream	O
.	O
</s>
<s>
When	O
the	O
input	O
file	O
reaches	O
end	O
of	O
file	O
,	O
the	O
command	B-Application
is	O
null	O
,	O
and	O
no	O
value	O
is	O
added	O
to	O
the	O
stack	O
.	O
</s>
<s>
The	O
input	O
lines	O
can	O
also	O
be	O
complex	O
dc	B-Application
commands	O
.	O
</s>
<s>
Note	O
that	O
since	O
dc	B-Application
supports	O
arbitrary	B-Algorithm
precision	I-Algorithm
,	O
there	O
is	O
no	O
concern	O
about	O
numeric	O
overflow	O
or	O
loss	O
of	O
precision	B-Architecture
,	O
no	O
matter	O
how	O
many	O
lines	O
the	O
input	O
stream	O
contains	O
,	O
unlike	O
a	O
similarly	O
concise	O
solution	O
in	O
AWK	B-Application
.	O
</s>
<s>
Downsides	O
of	O
this	O
solution	O
are	O
:	O
the	O
loop	O
stops	O
on	O
encountering	O
a	O
blank	O
line	O
in	O
the	O
input	O
stream	O
(	O
technically	O
,	O
any	O
input	O
line	O
which	O
does	O
not	O
add	O
at	O
least	O
one	O
numeric	O
value	O
to	O
the	O
stack	O
)	O
;	O
and	O
,	O
for	O
handling	O
negative	O
numbers	O
,	O
leading	O
instances	O
of	O
'	O
-	O
'	O
to	O
denote	O
a	O
negative	O
sign	O
must	O
be	O
change	O
to	O
'	O
_	O
 '	O
in	O
the	O
input	O
stream	O
,	O
because	O
of	O
dc	B-Application
's	O
nonstandard	O
negative	O
sign	O
.	O
</s>
<s>
operator	O
in	O
dc	B-Application
does	O
not	O
provide	O
a	O
clean	O
way	O
to	O
discern	O
reading	O
a	O
blank	O
line	O
from	O
reading	O
end	O
of	O
file	O
.	O
</s>
<s>
As	O
an	O
example	O
of	O
a	O
relatively	O
simple	O
program	O
in	O
dc	B-Application
,	O
this	O
command	B-Application
(	O
in	O
1	O
line	O
)	O
:	O
</s>
<s>
There	O
exist	O
also	O
quines	O
in	O
the	O
programming	O
language	O
dc	B-Application
;	O
programs	O
that	O
produce	O
its	O
source	O
code	O
as	O
output	O
.	O
</s>
<s>
An	O
implementation	O
of	O
the	B-Algorithm
Chudnovsky	I-Algorithm
algorithm	I-Algorithm
in	O
the	O
programming	O
language	O
dc	B-Application
.	O
</s>
<s>
A	O
more	O
complex	O
example	O
of	O
dc	B-Application
use	O
embedded	O
in	O
a	O
Perl	B-Language
script	O
performs	O
a	O
Diffie	B-Protocol
–	I-Protocol
Hellman	I-Protocol
key	I-Protocol
exchange	I-Protocol
.	O
</s>
<s>
This	O
was	O
popular	O
as	O
a	O
signature	O
block	O
among	O
cypherpunks	O
during	O
the	O
ITAR	O
debates	O
,	O
where	O
the	O
short	O
script	O
could	O
be	O
run	O
with	O
only	O
Perl	B-Language
and	O
dc	B-Application
,	O
ubiquitous	O
programs	O
on	O
Unix-like	B-Operating_System
operating	I-Operating_System
systems	I-Operating_System
:	O
</s>
<s>
A	O
commented	O
version	O
is	O
slightly	O
easier	O
to	O
understand	O
and	O
shows	O
how	O
to	O
use	O
loops	O
,	O
conditionals	O
,	O
and	O
the	O
q	O
command	B-Application
to	O
return	O
from	O
a	O
macro	O
.	O
</s>
<s>
With	O
the	O
GNU	O
version	O
of	O
dc	B-Application
,	O
the	O
|	O
command	B-Application
can	O
be	O
used	O
to	O
do	O
arbitrary	B-Algorithm
precision	I-Algorithm
modular	O
exponentiation	O
without	O
needing	O
to	O
write	O
the	O
X	O
function	O
.	O
</s>
