55

What are the differences between dc and bc calculators?

When should I use dc and when bc?

syntagma
  • 12,091
  • 21
  • 57
  • 74

4 Answers4

56

dc is a very archaic tool and somewhat older than bc. To quote the Wikipedia page:

It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax.

The syntax is a reverse polish notation, which basically means that the arguments (ie numbers) come first followed by the operator. A basic example of the dc usage is:

echo '3 4 * p' | dc

Where the p is required to print the result of the calculation. bc on the other hand uses the more familiar infix notation and thus is more intuitive to use. Here is an example of bc usage:

echo '3 * 4' | bc

Which one to use?

bc is standardised by POSIX and so is probably the more portable of the two (at least on modern systems). If you are doing manual calculator work then it is definitely the choice (unless you are somewhat of a masochist). dc can still have its uses though, here is a case where the reverse polish notation comes in handy. Imagine you have a program which outputs a stream of numbers that you want to total up, eg:

23
7
90
74
29

To do this with dc is very simple (at least with modern implementations where each operator can take more than two numbers) since you only have to append a +p to the stream, eg:

{ gen_nums; echo +p } | dc

But with bc it is more complex since we not only need to put a + between each number and make sure everything is on the same line, but also make sure there is a newline at the end:

{ gen_nums | sed '$ !s/$/+/' | tr -d '\n'; echo; } | bc
Graeme
  • 33,607
  • 8
  • 85
  • 110
  • 14
    You've turned the `gen_nums` example horribly complex using `bc`. It could be simplified to: `gen_nums | paste -sd+ | bc` – devnull Apr 13 '14 at 10:22
  • 19
    Fun fact: Traditionally `bc` was only a front end tool that compiled the `bc` notation to the notation of `dc` and piped that into `dc` to get the result. On FreeBSD there is still the `-c` flag for `bc` with which you can still compile to the `dc` notation`. For example `echo '3 * 4' | bc` is equal to `echo '3 * 4' | bc -c | dc`. See the http://linux.die.net/man/1/bc and http://www.freebsd.org/cgi/man.cgi?query=bc&sektion=1 – Raphael Ahrens Apr 14 '14 at 10:24
  • 18
    Hmm, which `dc` are you using? On Ubuntu 14.04 and OS X, the `+` operator always operates on exactly two values off the stack – Digital Trauma Mar 25 '15 at 19:01
  • 2
    The correct example of `dc` usage: `gen_nums_nonnegative | dc -e '0 0 [+?z1 – ruvim Oct 19 '15 at 15:08
  • 1
    @devnull I for one almost never remember that `paste` exists, and I have been using *nix for over a decade now. – Wyatt Ward Mar 25 '18 at 02:21
  • 1
    @RaphaelAhrens Interesting, on macOS `bc -c` does something weird and different (and the `-c` flag is undocumented). If I try to pipe it into `dc`, I get a bunch of errors (and the output without piping into `dc` is the very cryptic `@iK3:K4:*W@r\n@i`). – Konrad Rudolph Jun 26 '18 at 13:00
  • @KonradRudolph What is the output of `bc --version`? – Raphael Ahrens Jun 26 '18 at 13:34
  • @RaphaelAhrens 1.0.6. And `dc` gives me: `dc (GNU bc 1.06) 1.3` — which is weird: why would dc be built on top of bc (rather than the other way round)? – Konrad Rudolph Jun 26 '18 at 14:13
  • @KonradRudolph because GNU only has `bc` and it looks like OSX uses GNU `bc` and maybe uses a wrapper or links to `bc` to implement `dc`. Sadly I can't find the man pages of `bc` and `dc` for OSX online, to verify this. – Raphael Ahrens Jun 26 '18 at 14:36
  • @RaphaelAhrens The manpages don’t mention anything along these lines, apart from both claiming to be the GNU versions. – Konrad Rudolph Jun 26 '18 at 14:38
  • 1
    @KonradRudolph simple anwser. `bc` and `dc` are packaged together under GNU. So when you download the sources for GNU `bc` you get `dc` as well, but they seam to have different verions and only the `bc` version number is referenced when downloading. – Raphael Ahrens Jun 26 '18 at 14:48
  • FWIW, the `-c` option for GNU `bc` does dump the bytecode it generates, but as the docs for GNU `bc` say: "Since the POSIX document does not specify how bc must be implemented, this version does not use the historical method of having bc be a compiler for the dc calculator. This version has a single executable that both compiles the language and runs the resulting `byte code'. The byte code is not the dc language." – Michael Burr Jul 29 '19 at 20:24
  • What is `gen_nums` referring to? – Diagon Jun 22 '20 at 00:58
  • 1
    @Diagon, just supposed to be a placeholder for something generating a stream of numbers. If you want to test the examples, you could use something like `seq -s ' ' 1 10`. – Graeme Jun 24 '20 at 16:17
  • Real UNIX bc is actually a preprocessor for dc(1), which it invokes automatically, unless the -c (compile only) option is present. If you invoke bc with the flag -c it will spit out dc commands to the standard output – Predrag Punosevac Dec 26 '21 at 19:03
10

A basic difference between the two is that dc uses the reverse Polish notation. It requires explicit commands even in order to produce an output.

You might add two integers in bc by saying:

bc <<< "2+4"

and it would produce 6 on a line by itself. However, in dc you'd need to say:

dc <<< "2 4 +p"

You can also do much fun stuff using dc, e.g. refer to my answer here for producing

Hello World!

using dc.

dc<<<"8 9*P101P108P108P111P4 8*P81 6+P111P114P108P100P33P"

devnull
  • 10,541
  • 2
  • 40
  • 50
3

dc is a calculator whereas bc is an actual language. See their man pages.

dc

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 before reading from standard input. All normal output is to standard output; all error output is to standard error.

bc

bc is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming language. A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input. All code is executed as it is read. (If a file contains a command to halt the processor, bc will never read from the standard input.)

It really depends on what you're ultimately wanting to do mathematically. Some operations aren't possible using dc. I've used both over the years in addition to several other command-line calculator tools. See "Command line expression solver?" for some additional examples.

slm
  • 363,520
  • 117
  • 767
  • 871
  • Thanks, one more question: how is `bc` command and its language used in practice nowadays? – syntagma Apr 13 '14 at 09:47
  • 3
    @REACHUS - I'm not sure I understand what you're asking? Due to `dc` being a reverse polish notation calculator and since I've always used HP reverse polish notation calculators I actually prefer using `dc` to `bc` but I would say they're both still used anytime you need to perform a complex mathematical calculation. I often use them to convert numbers b/w base 10 to base 16 and base 2. But have used them to tally columns in tabular data as well from within an AWK script. – slm Apr 13 '14 at 12:23
  • 3
    `bc` was a *language* in the sense that it made `dc` human-friendly - not that it was more powerful. These days the apps share libs, but `bc` was called a *language* and `dc` a *calculator* because `dc` did math and `bc` spoke math - to `dc`. [Here's an older `man` page](http://plan9.bell-labs.com/7thEdMan/vol2/dc): *A language called `BC` [1] has been developed which accepts programs written in higher-level languages and compiles output which is interpreted by DC. Some of the commands described below were designed for the compiler interface and are not easy for a human user to manipulate.* – mikeserv Feb 15 '15 at 14:34
  • 2
    I claim `dc` is also a language. e.g. http://codegolf.stackexchange.com/a/47921/11259 – Digital Trauma Mar 25 '15 at 19:02
  • 1
    @DigitalTrauma To argue that it isn't a language would be absurd since it has a lexer and parser. But to argue it's human readable, given that example, would be absurd. I mean... damn. – Parthian Shot Jul 06 '15 at 22:22
  • 1
    @ParthianShot Agreed. My reading of *"dc is a calculator whereas bc is an actual language"* is that slm is implying that dc is not a language, which I am disagreeing with. But I might just be reading into the answer wrong... – Digital Trauma Jul 06 '15 at 23:11
  • @DigitalTrauma - based on the wikipedia page's descriptions, dc is described as a calculator. Those are excerpts from the 2 pages, not my own words. I was simply echoing their sentiments. But it's a point that I would agree w/. The big problem is the blurring of the lines b/w "what is a calculator" and what is not. – slm Jul 07 '15 at 15:04
  • @slm IMO `dc` is a calculator *and* a language - as is `bc` - there is no distinction between the two in this regard. The `bc` language just happens to be more readable (and writeable). – Digital Trauma Jul 07 '15 at 15:19
  • `dc` is like brainfuck but a version that was intended to be somewhat legible to a mathematician who worked on a PDP-7 in the 1960s. Still a language. – Wyatt Ward Jun 19 '16 at 21:42
0

In my humble opinion and in as few words as possible, GNU dc differs from GNU bc in 3 ways:

1) If bc's math library is out of concern, to save as many keystrokes as possible (in Jan Łukasiewicz's words: a parenthesis-free notation) when doing elementary arithmetic, to concentrate more on the essentials and to avoid distractions.

2) In Microsoft Windows 7's command line, GNU dc consumes less memory (~ 320K) than GNU bc does (~ 360K).

3) Much granular control in GNU dc than in GNU bc, for example, stack and register commands are absent from GNU bc.

Version 1.07.1 win32 for both is at https://embedeo.org/ws/command_line/bc_dc_calculator_windows/

bc_vs._dc
  • 1
  • 1