dc

Created: 11 Jan 2025
Updated: 24 Apr 2025
manual gnu bsd
source unix
wiki english spanish

cli

dc [OPTIONS] [FILES]
-f FILE executes script file
–file=FILE "
-e SCRIPT runs commands in script on input
–expression=SCRIPT "
DC_LINE_LENGTH=0 disable line break
$ dc script.dc
$ dc -e '[Enter time: ]P?st' -e '[Distance: ]Plt2^0.5*9.8*p'
$ echo '2 3 +' | dc
$ echo '2 3'   | dc -f - -e 'f'
$ echo '2 3'   | dc -e '? ? +'

language

  • supports recursion
  • # for comments

strings

  • can only be printed or executed as macros
  • delimited by []

numbers

  • hex numbers MUST be upcased (A-F)
  • use _ for negative numbers
T   P   T   P  
1 i   set input radix   I 1 push input radix
1 o   set output radix   O 1 push output radix
1 k   set decimal precision   K 1 push decimal precision
1 Z 1 number of decimals 1 X 1 number of fraction digits
2 + 1 fst + snd 2 - 1 fst - snd
2 * 1 fst * snd 2 / 1 fst / snd
2 % 1 remainder of / 2 ~ 2 divmod(fst,snd)
2 ^ 1 fst ^ snd 3 ¦ 1 mod(fst, snd^trd)
1 v 1 sqrt        

arrays

2 :A   A[snd] = fst
1 ;A 1 A[fst]

registers (256)

  • each register has his own stack
  • valid names: a A 0 . " …
    • one char length
    • you CAN use a register name that overlaps with a command
    • and the command will stil work
1 sR   R.push(fst) 1 SR   R=[fst]
  LR 1 R.pop()   lR 1 R[R.size]¦¦0
2 >R   x R if > 2 <R   x R if <
2 !<R   x R if >= 2 !>R   x R if <=
2 =R   x R if = 2 !=R   x R if !=

other commands

1 a 1 1st char/byte
  !   system command
  #   comment line
  ? 1 ask user input, and x it
1 x   executes as a macro
  q   exits 2 level macro
1 Q   exits Nth level macro

print

  f   print full stack
  p   print top of stack, with \n
1 n   print top of stack, no \n
1 P 1 print top of stack, no \n, as char if number

stack manipulation

  z 1 stack size
* c   clear all stack
1 d 2 dup
2 r 2 swap
1 R   rotates top Nth items once

snippets

morrischerry74-bright.png
Figure 1: Morris and Cherry on 1974

codebases

sergiosgc/AdventOfCode2021

  • day1 part 1

    [1Lc+sc]sC
    [rdSar>CLaz1<L]sL
    0sc0d=LLcp
    
  • day1 part 2

    [lCsDlBsClAsBsA]sr
    [1lR+sR]si
    [lBlClD++lAlBlC++<i0d=rz0!=l]sl
    0sA0sB0sC0sD0sR
    0d=r0d=r0d=r0d=r
    0d=l0dd=llRp
    

tckmn/polyaoc-2019

articles

videos

implementations