Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Dc (computer program)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Language features== ===Registers=== In addition to these basic arithmetic and stack operations, dc includes support for [[Macro (computer science)|macros]], conditionals and storing of results for later retrieval. The mechanism underlying macros and conditionals is the '''register''', which in dc is a storage location with a single character name which can be stored to and retrieved from: <code>sc</code> pops the top of the stack and stores it in register c, and <code>lc</code> pushes the value of register c onto the stack. For example: <pre> 3 sc 4 lc * p </pre> Registers can also be treated as secondary stacks, so values can be pushed and popped between them and the main stack using the <code>S</code> and <code>L</code> commands. ===Strings=== String values are enclosed in <code>[</code> and <code>]</code> characters and may be pushed onto the stack and stored in registers. The <code>a</code> command converts the low order byte of the numeric value into an [[ASCII#ASCII printable characters|ASCII]] character, or if the top of the stack is a string it replaces it with the first character of the string. There are no ways to build up strings or perform string manipulation other than executing it with the <code>x</code> command, or printing it with the <code>P</code> command. The <code>#</code> character begins a comment to the end of the line. ===Macros=== Macros are then implemented by allowing registers and stack entries to be strings as well as numbers. A string can be printed, but it can also be executed (i.e. processed as a sequence of dc commands). So for instance we can store a macro to add one and then multiply by 2 into register m: <pre> [1 + 2 *] sm </pre> and then (using the <code>x</code> command which executes the top of the stack) we can use it like this: <pre> 3 lm x p </pre> ===Conditionals=== Finally, we can use this macro mechanism to provide conditionals. The command <code>=r</code> pops two values from the stack, and executes the macro stored in register <code>r</code> only if they are equal. So this prints the string <code>equal</code> only if the top two values on the stack are of equal value: <pre> [[equal]p] sr 5 5 =r </pre> Other conditionals are <code>></code>, <code>!></code>, <code><</code>, <code>!<</code>, <code>!=</code>, which execute the specified macro if the top two values on the stack are greater, less than or equal to ("not greater"), less than, greater than or equal to ("not less than"), and not equals, respectively. Note that the order of the operands in inequality comparisons is the opposite of the order for arithmetic; {{kbd|5 3 -}} evaluates to {{code|5 - 3 {{=}} 2}}, but {{kbd|5 3 <t}} runs the contents of the {{kbd|t}} register because {{code|3 < 5}}. ===Loops=== Looping is then possible by defining a macro which (conditionally) reinvokes itself. A simple factorial of the top of the stack might be implemented as: <pre> # F(x): return x! # if x-1 > 1 # return x * F(x-1) # otherwise # return x [d1-d1<F*]dsFxp </pre> The <code>1Q</code> command exits from a macro, allowing an early return. <code>q</code> quits from two levels of macros (and dc itself if there are less than two levels on the call stack). <code>z</code> pushes the current stack depth before the <code>z</code> operation.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)