Befunge Techniques

Printing Strings with Quote Mode

How Befunge's double-quote instruction pushes character codes, why strings come out backward, and three reliable ways to print text in the right order.

On this page
  1. What the double quote does
  2. Why text comes out backward
  3. Method 1: write the string backward
  4. Method 2: print in a loop until a marker
  5. Method 3: let a routine reverse the text
  6. Newlines and special characters
  7. Common mistakes
  8. Where this fits

What the double quote does

Befunge has no string type. What it has is a mode. When the pointer executes a double quote, it switches into string mode, and from then on every cell it passes over is not executed. Instead, the character code in that cell is pushed onto the stack. The next double quote switches the mode off again, and normal execution resumes.

So "Hi" pushes two numbers: 72 for H and then 105 for i. Spaces are pushed too, as 32. Nothing is printed. The characters now sit on the program stack as plain integers, and you decide what to do with them. This is why the instruction is better thought of as “push the cells I pass over” than as a string literal.

Why text comes out backward

The stack is last in, first out. After "Hi", the i is on top. The comma instruction pops the top value and prints it as a character, so the first comma prints i and the second prints H. The text appears reversed.

This surprises almost everyone once. The fix is to compensate on purpose, and there are three common ways.

Method 1: write the string backward

If the string is short and fixed, store it reversed in the source. To print Hi!, push !, then i, then H:

"!iH",,,@

The pointer enters string mode, pushes the three codes, leaves string mode, and then three commas pop and print H, i and ! in that order. The @ ends the program.

This is the simplest method and the right one for very short messages. Its drawbacks are that the source is hard to proofread and that you must count the commas correctly. Too few commas leave characters on the stack, and too many cause a stack underflow, which often just yields a zero value instead of an obvious error.

Method 2: print in a loop until a marker

For longer text, push a zero first as an end marker. A zero is not a character that you would normally print, so it is a convenient sentinel. Then push the reversed string on top of it and loop, printing until the marker comes up.

0"!iH">:#,_@

The first 0 goes on the stack, then the quote mode pushes the reversed text. After that, a small loop duplicates the top value, tests it, and either prints and repeats or stops. The details of that skip-based loop are explained in the guide to the trampoline instruction. The important point here is that the number of commas no longer matters: the loop prints until the marker appears. It is the version to use when you edit the text often, since changing the message does not require changing the code.

Be careful that the string cannot contain a character with code zero, because it would end the loop early. Normal text is safe.

Method 3: let a routine reverse the text

If you would rather write the text in natural order, you can push it as written and then reverse the stack contents with a small routine before printing. That costs several extra rows of code and is rarely worth it for a fixed message, but it is the right idea when text arrives from input and must come out in the other order. The string reversal algorithm is a good exercise in that direction. A different route is to store characters in the grid and read them back one at a time with the get instruction, which is the subject of the guide on grid variables.

Newlines and special characters

A line break cannot be typed inside a string, because each cell holds one character and the pointer moves along the row. To print a newline, push the number 10 with arithmetic, such as 55+, and print it with a comma.

Push it before the text if you want it printed last. For example, 55+"!iH",,,,@ pushes 10, then the reversed text, then prints four characters: H, i, ! and finally the newline.

The double quote itself cannot appear inside a string, because it would end the mode. To print one, build its character code, 34, with arithmetic. One way is 94*2-, which computes 9 times 4 minus 2. Then print it with a comma.

Common mistakes

  • Forgetting the closing quote. The pointer stays in string mode and pushes the rest of the row, including instructions you meant to run, then wraps around and continues.
  • Writing the text in natural order and wondering why the output is reversed.
  • Miscounting commas in the backward method.
  • Leaving a marker or text on the stack after a program finishes a section, so that the next section sees unexpected data.

These are all common beginner mistakes, and a trace of the stack after each step finds every one of them.

Where this fits

Printing a message is usually the first thing anyone does in a new language, and in Befunge it is already a lesson in how the stack works. If you want a complete small program built on these ideas, the Hello World program page extends the same approach to a longer message. Once you are comfortable with the order of characters, the same understanding helps when you read input, because characters you read also land on the stack in the order they arrive.

The complete guide to befunge techniques

Befunge Programming Techniques Overview
Read the guide

Keep exploring

Guides from other topics

All topics →