Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What are the differences between dc and bc calculators?

When should I use dc and when bc?

share|improve this question
    
I use expr very often for simple calculations on the command line and in scripts. –  MattBianco Apr 25 at 14:47

3 Answers 3

up vote 4 down vote accepted

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
share|improve this answer
2  
You've turned the gen_nums example horribly complex using bc. It could be simplified to: gen_nums | paste -sd+ | bc –  devnull Apr 13 at 10:22
    
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 linux.die.net/man/1/bc and freebsd.org/cgi/man.cgi?query=bc&sektion=1 –  Raphael Ahrens Apr 14 at 10:24

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.

share|improve this answer
    
Thanks, one more question: how is bc command and its language used in practice nowadays? –  REACHUS Apr 13 at 9:47
    
@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 at 12:23

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"

share|improve this answer
    
"Bc was written as a front-end to Dc" –  Adam Apr 25 at 15:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.