Befunge
A two-dimensional esoteric programming language where code flows through a grid in multiple directions, designed to be as difficult to compile as possible.
Created by Chris Pressey
Befunge is a two-dimensional esoteric programming language created by Chris Pressey in 1993. Unlike traditional languages where code flows linearly from top to bottom, Befunge programs exist on a grid where execution can travel in any cardinal direction - right, left, up, or down.
History & Origins
Chris Pressey created Befunge in 1993 for the Amiga with a very specific goal in mind: to design a language that would be as difficult to compile as possible. The two-dimensional nature and self-modifying code capabilities make static analysis extremely challenging.
The Name “Befunge”
The word “Befunge” originated as a typo. During a late-night BBS chat session (around 4 AM), Curtis Coleman accidentally typed “Befunge” instead of “before.” Pressey encountered this typo and immediately decided it was the perfect name for a programming language he wanted to create.
Influence on Esoteric Languages
Befunge is believed to be the first two-dimensional, ASCII-based, general-purpose programming language. Its creation helped establish the “esolang” (esoteric language) community. The Befunge Mailing List, which Pressey started in 1995, eventually evolved into the Esoteric Topics Mailing List, helping to formalize the concept of esoteric programming languages.
How It Works
The Playfield
A Befunge-93 program exists on an 80x25 grid of ASCII characters called the “playfield.” Each cell contains a single instruction, and the program counter (called the “instruction pointer” or IP) navigates through this grid.
The grid is toroidal, meaning if the IP moves off one edge, it wraps around to the opposite edge - like a video game screen.
The Instruction Pointer
The IP starts at position (0,0) - the top-left corner - moving right. It continues in its current direction until it encounters a direction-changing instruction.
The Stack
Befunge uses a stack for data storage, similar to Forth. Most operations push values onto or pop values from this stack. The stack can hold arbitrary integers.
Commands
Befunge-93 has a compact instruction set:
Direction Commands
| Command | Description |
|---|---|
> | Move right |
< | Move left |
^ | Move up |
v | Move down |
? | Move in a random direction |
Math Operations
| Command | Description |
|---|---|
+ | Addition: pop a and b, push a+b |
- | Subtraction: pop a and b, push b-a |
* | Multiplication: pop a and b, push a*b |
/ | Division: pop a and b, push b/a |
% | Modulo: pop a and b, push b%a |
Stack Operations
| Command | Description |
|---|---|
0-9 | Push the digit onto the stack |
: | Duplicate top of stack |
\ | Swap top two values |
$ | Discard top of stack |
Logic & Control
| Command | Description |
|---|---|
! | Logical NOT: pop value, push 1 if zero, else 0 |
` | Greater than: pop a and b, push 1 if b>a, else 0 |
_ | Horizontal if: pop value, go right if zero, left if non-zero |
| ` | ` |
Input/Output
| Command | Description |
|---|---|
. | Output top of stack as integer |
, | Output top of stack as ASCII character |
& | Input integer and push |
~ | Input character and push ASCII value |
String Mode
| Command | Description |
|---|---|
" | Toggle string mode: push ASCII values of characters until next " |
Self-Modification
| Command | Description |
|---|---|
g | Get: pop y and x, push value at (x,y) in playfield |
p | Put: pop y, x, and v, write v to (x,y) in playfield |
Control
| Command | Description |
|---|---|
# | Bridge: skip next cell |
@ | End program |
| Space: no-op (do nothing) |
Versions
Befunge-93
The original specification with:
- Fixed 80x25 playfield
- Limited to what can be expressed in this grid
- Not technically Turing-complete due to finite memory
Funge-98
The extended specification (1998) that removes limitations:
- Unlimited playfield size (making it Turing-complete)
- Additional instructions
- Support for higher dimensions (Unefunge-98 for 1D, Trefunge-98 for 3D)
- Concurrent execution support
Why Befunge Matters
1. Compilation Challenge
Befunge’s explicit design goal of being “difficult to compile” makes it an interesting case study. The self-modifying code (via p command) means the program can rewrite itself during execution, making static analysis nearly impossible.
2. Spatial Programming
Befunge pioneered the concept of truly two-dimensional code. The visual layout of a Befunge program matters - you can often trace execution paths by following the direction arrows.
3. Esoteric Language Foundation
As one of the earliest and most influential esoteric languages, Befunge helped establish a community of language designers who create languages for artistic, educational, or humorous purposes rather than practical use.
File Extensions
Befunge source files typically use:
.bf- Common (shared with Brainfuck, so context matters).b93- Befunge-93 specific.b98- Funge-98 specific.befunge- Explicit
Running Befunge
Many interpreters exist:
- befunge93 - Reference implementation
- cfunge - Fast C implementation supporting both 93 and 98
- PyFunge - Python implementation
- BeQunge - Visual debugger for Befunge
For this tutorial, we use the esolang/befunge93 Docker image for consistent cross-platform execution.
A Word of Caution
Befunge is intentionally challenging. It’s designed for:
- Exploring unusual programming paradigms
- Compiler theory discussions
- Programming challenges and puzzles
- Having fun with constraints
It is not designed for:
- Production code
- Practical applications
- Teaching beginners to program
Continue to the Hello World tutorial to write your first Befunge program.
Timeline
Notable Uses & Legacy
Compiler Design Challenges
Used to explore the limits of compilation, as self-modifying code makes static analysis nearly impossible.
Code Golf
Popular in programming challenges due to its unique 2D nature and concise instruction set.
Esoteric Language Community
Foundational language that helped establish the 'esolang' community and inspired many other 2D languages.
Programming Puzzles
Used in CTF competitions and puzzle challenges for its unusual execution model.
Language Influence
Influenced By
Influenced
Running Today
Run examples using the official Docker image:
docker pull esolang/befunge93Example usage:
docker run --rm -v $(pwd):/code:ro esolang/befunge93 befunge93 /code/hello.bf