dc, an arbitrary precision calculator - 8. Strings

Go to the first, previous, next, last section, table of contents.

# 8. Strings

DC can operate on strings as well as on numbers. The only things you can do with strings are print them and execute them as macros (which means that the contents of the string are processed as DC commands). Both registers and the stack can hold strings, and DC always knows whether any given object is a string or a number. Some commands such as arithmetic operations demand numbers as arguments and print errors if given strings. Other commands can accept either a number or a string; for example, the ``p'` command can accept either and prints the object according to its type.

``[characters]'`
  Makes a string containing characters and pushes it on the stack. For example, ``[foo]P'` prints the characters ``foo'` (with no newline).
``a'`
  The mnemonic for this is somewhat erroneous: asciify. The top-of-stack is popped. If it was a number, then the low-order byte of this number is converted into a string and pushed onto the stack. Otherwise the top-of-stack was a string, and the first character of that string is pushed back. (This command is a GNU extension.)
``x'`
  Pops a value off the stack and executes it as a macro. Normally it should be a string; if it is a number, it is simply pushed back onto the stack. For example, ``[1p]x'` executes the macro ``1p'`, which pushes 1 on the stack and prints ``1'` on a separate line. Macros are most often stored in registers; ``[1p]sa'` stores a macro to print ``1'` into register ``a'`, and ``lax'` invokes the macro.
``>r'`
  Pops two values off the stack and compares them assuming they are numbers, executing the contents of register r as a macro if the original top-of-stack is greater. Thus, ``1 2>a'` will invoke register ``a'`'s contents and ``2 1>a'` will not.
``!>r'`
  Similar but invokes the macro if the original top-of-stack is not greater (is less than or equal to) what was the second-to-top.
``<r'`
  Similar but invokes the macro if the original top-of-stack is less.
``!<r'`
  Similar but invokes the macro if the original top-of-stack is not less (is greater than or equal to) what was the second-to-top.
``=r'`
  Similar but invokes the macro if the two numbers popped are equal.
``!=r'`
  Similar but invokes the macro if the two numbers popped are not equal.
``?'`
  Reads a line from the terminal and executes it. This command allows a macro to request input from the user.
``q'`
  During the execution of a macro, this command exits from the macro and also from the macro which invoked it. If called from the top level, or from a macro which was called directly from the top level, the ``q'` command will cause DC to exit.
``Q'`
  Pops a value off the stack and uses it as a count of levels of macro execution to be exited. Thus, ``3Q'` exits three levels.

Go to the first, previous, next, last section, table of contents.