dc

calculatorlinux
The dc command is one of the most frequently used commands in Linux/Unix-like operating systems. dc The dc command is an arbitrary precision calculator with reverse Polish notation. It supports unlimited precision arithmetic and features both interactive and script-based usage.

Quick Reference

Command Name:

dc

Category:

calculator

Platform:

linux

Basic Usage:

dc -e "5 5 + p"

Common Use Cases

  • 1

    Complex calculations

    Perform arbitrary-precision arithmetic calculations using RPN notation

  • 2

    Scripted math

    Execute mathematical operations in scripts without floating-point errors

  • 3

    Base conversion

    Convert numbers between different number bases (decimal, hex, octal, etc.)

  • 4

    Stack manipulation

    Work with stack-based data structures for complex operations

Syntax

dc [options] [file...]

Options

Option Description
-e expr Evaluate expression
-f file Read commands from file
-h, --help Display help information and exit
-V, --version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the dc command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Start dc in interactive mode
dc
Basic addition (5 + 3)
echo "5 3 + p" | dc
Basic subtraction (7 - 2)
echo "7 2 - p" | dc
Basic multiplication (4 * 6)
echo "4 6 * p" | dc
Basic division (20 / 4)
echo "20 4 / p" | dc

Advanced Examples:

Calculate with arbitrary precision (scale)
echo "10 k 1 3 / p" | dc
Calculate square root of 2 to 20 decimal places
echo "20 k 2 v p" | dc
Use registers to store values
echo "5 sa 10 sb la lb + p" | dc
Factorial calculation (5!)
echo "5 [d 1 - d 0 != f*]sf 1 5 f p" | dc
Exponentiation (2^10)
echo "2 10 ^ p" | dc
Converting from decimal to hexadecimal
echo "16 o 255 p" | dc
Converting from hexadecimal to decimal
echo "16i FF p" | dc
Calculate pi to 20 decimal places
echo "20 k 4 1 3 / 1 5 / - 1 7 / + 1 9 / - 1 11 / + 1 13 / - 1 15 / + * p" | dc

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Stack Operations:

dc is a stack-based calculator. The stack operations include:

  • p - Print the top value on the stack
  • f - Print the entire stack
  • c - Clear the stack
  • d - Duplicate the top value
  • r - Reverse the top two values
  • z - Push the stack size

Arithmetic Operations:

Basic arithmetic operations:

  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division
  • % - Remainder
  • ^ - Exponentiation
  • v - Square root

Registers:

dc provides 256 registers that can store values:

  • sx - Store the value in register x
  • lx - Load the value from register x
  • Sx - Store the value in register x and pop it from the stack
  • Lx - Copy the value from register x and push it onto the stack

Scale Setting:

You can set the precision for decimal arithmetic:

  • k - Set the precision (scale) to the value on the stack
  • K - Push the current precision (scale) onto the stack

Input/Output Bases:

dc can work with different number bases:

  • i - Set input base to the value on the stack (default 10)
  • o - Set output base to the value on the stack (default 10)
  • I - Push the current input base onto the stack
  • O - Push the current output base onto the stack

Macros and Conditional Execution:

dc supports macros (strings of commands) and conditional execution:

  • [ ... ] - Define a macro
  • x - Execute the macro stored in register x
  • >, <, = - Conditional operators

Comparison with bc:

While dc uses reverse Polish notation, bc provides a more conventional infix notation:

  • dc is more primitive but offers all features directly
  • bc is more user-friendly but is actually implemented using dc
  • bc offers variables with names longer than one character
  • For most interactive use cases, bc is easier to use

Tips & Tricks

1

Use the -e expression option to evaluate an expression

2

Use the -f file option to read expressions from a file

3

Use the -s option to suppress the output of intermediate results

4

Use the -V option to display version information

5

Use the -h option to display help

Common Use Cases

Complex calculations

Perform arbitrary-precision arithmetic calculations using RPN notation

Scripted math

Execute mathematical operations in scripts without floating-point errors

Base conversion

Convert numbers between different number bases (decimal, hex, octal, etc.)

Stack manipulation

Work with stack-based data structures for complex operations

Programming exercises

Learn and practice reverse Polish notation and stack-based programming

Related Commands

These commands are frequently used alongside dc or serve similar purposes:

Use Cases

1

Complex calculations

Perform arbitrary-precision arithmetic calculations using RPN notation

2

Scripted math

Execute mathematical operations in scripts without floating-point errors

3

Base conversion

Convert numbers between different number bases (decimal, hex, octal, etc.)

4

Stack manipulation

Work with stack-based data structures for complex operations

5

Programming exercises

Learn and practice reverse Polish notation and stack-based programming

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the dc command works in different scenarios.

$ dc
View All Commands
dc - Linux Command Guide