Open In App

dc command in Linux with examples

Last Updated : 22 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The dc command is a versatile calculator found in Linux systems, operating using reverse Polish notation (RPN). This command allows users to perform arithmetic calculations and manipulate a stack, making it ideal for complex mathematical tasks directly from the command line.

Syntax

The basic syntax for using the dc command is as follows:

dc [OPTION] [file ...]

Examples of dc Command

Example 1: Basic Addition

To illustrate how dc works, consider this simple example:

echo "5 3 + p" | dc

Output: 8

In this case, the command adds 5 and 3, then prints the result.

Example 2: Basic Arithmetic Operations

Basic Arithmetic Operations

Example 3: Changing Output Radix

Changing Output Radix

Note: In second example, the output changes because we changed the output-radix. 

Key Options Commonly Used with the Command

The dc command includes several options and operations for stack manipulation. Below is an overview of some essential commands:

Command Operation
p Prints the value on the top of the stack and ends the statement with a newline.
n Prints the value on the top of the stack and ends the line with a null statement.
f Prints the entire stack, without any alteration.
P Pops the value from the top of the stack.
c Clear the stack.
d Duplicates the top value and push it into the main stack.
r Reverses the order of top two elements in the stack.
Z Pops the value from the stack, calculate the number of digits in it and pushes that number.
X Pops the value from the stack, calculate the number of fraction digits in it and pushes that number.
z Pushes the stack length into the stack.
i Pops the value from the stack and uses it as input radix.
o Pops the value from the stack and uses it as output radix.
k Pops the values from the stack and uses it to set precision.
I Pushes the value of input radix into the stack.
O Pushes the value of output radix into the stack
K Pushes the precision value into the stack.
  • expression: This command is used to evaluate a particular expression.

expression

  • file: This command is used to evaluate the contents of the file.

file

  • help: This command displays help information.

help

  • version: This command is used to display version information.

version

Conclusion

The dc command provides a powerful means to perform arithmetic operations directly from the terminal. Its use of reverse Polish notation may take some getting used to, but its ability to handle arbitrary precision makes it a valuable tool for both simple calculations and more complex mathematical tasks.

dc command in Linux – FAQs

What is reverse Polish notation (RPN)?

RPN is a notation in which operators follow their operands, removing the need for parentheses to define the order of operations.

How can I clear the stack in dc?

To clear the stack, simply use the command c.

Can dc handle very large numbers?

Yes, dc supports arbitrary-precision arithmetic, allowing it to work with very large numbers.

How do I modify the output format in dc?

You can change the output format using the o command, which sets the output radix for the numbers that follow.


Next Article

Similar Reads

three90RightbarBannerImg
  翻译: