LearnReferenceWriting programs
Writing real programs
Nobody writes a large brainfuck program by typing plus signs and hoping. People who write them by hand first draw a map of the tape, then put the program together from a small set of well-known pieces [1] [2]. People who want something larger write a program in another language that writes the brainfuck: with macros, or with a compiler from BASIC or C [3].
Think in cells
Other languages let you name things. Brainfuck has only places. So the first step of any program is to decide what lives where, and to write that down. Daniel Cristofani, who has written more well-known brainfuck by hand than anyone we know of, makes such maps of memory for his own programs and for other people's, and advises readers to follow "the way the data moves and changes over time" [1].
Here is the map for a program that reads a line and prints it backwards. Cell 0 stays 0 on purpose, so that a loop walking left knows where to stop:
Three habits make maps work:
- Say where the pointer is. For every piece of program, write down where the pointer is before and after. Most mistakes are a pointer one cell off.
- Leave gaps. Many pieces need a spare cell next to the data. Programs that handle lists often give every item two or three cells: the value and some room to work.
- Use zeros as fences. A cell that is always 0 stops a loop such as [<]. A row of data between two zeros can be walked from either end.
The program below is the one that the map is for: it reads characters into cells 1, 2, 3 and so on until a new line comes, then walks back to the left and prints each cell. Type another word (up to eight letters; the new line at the end is added for you) and press Play:
The program has two loops. The first reads a character and takes 10 off. If the result is 0, the character was a new line and the loop ends. If not, it puts the 10 back, moves right and reads the next one. The second loop, [.<], prints and steps left until it meets the 0 in cell 0. The fence was planned in the map before any command was written. (The program expects a new line at the end of its input: see the end of the input.)
The idioms
A few short pieces of program come back everywhere. Learn to read these six and most brainfuck stops looking like noise. The table lists them. The patterns section has each one as a page you can run: Set a cell to zero, Move a value, Copy a value, Multiply by a constant, If non-zero and Find a zero cell or a marker. The Esolang wiki keeps a longer cookbook, which also says for each recipe whether it needs cells that wrap [2].
| Name | Program | What it does |
|---|---|---|
| Clear | [-] | Makes x zero, whatever it was |
| Move | [->+<] | Adds x to y and leaves x empty |
| Copy | [->+>+<<]>>[-<<+>>]<< | Adds x to y and keeps x: it moves x to both y and t, then moves t back |
| Multiply | [->+++<] | Adds three times x to y and leaves x empty |
| If | [ ... [-]] | Runs the part at the dots once if x is not zero. The part must come back to x |
| Find a zero | [>] | Moves right to the first cell that holds zero. This one does not end where it began |
The program below copies cell 0 into cell 1, with cell 2 as a spare. Press Play, or drag Step to watch cell 2 fill up and empty again. Change the start value:
The only way to read a number is to count it down in a loop, and that destroys it. So copying takes two moves: one loop moves the number to two places at once, a second loop moves one of those back. Every idiom that "keeps" a value pays for it with a spare cell and a second loop.
Numbers
A number is made by adding. For 65 you could write sixty-five +, but eight times eight plus one is 23 commands. Programs that print text spend most of their length on this, and finding the shortest way to make each number is a sport of its own: the Esolang wiki has a table of the shortest known program for every number from 0 to 255 [4]. Many of the entries go below zero on purpose and need cells that wrap.
Comparing and dividing
There is no "less than" and no division. Both are built from the idioms in the table above: count two numbers down together and see which reaches zero first; subtract again and again and count how often. The standard recipes are in the cookbook [2] and in the patterns Less than and Divide, with remainder. They are the point where hand-written brainfuck gets hard, and where most people start to want a tool.
Laying out the source
Because every other character is ignored, a program can carry its own explanation. Good hand-written programs use this heavily: one idea per line, the map of the tape in a comment at the top, and after each piece a note saying where the pointer is. Linus Åkesson, who wrote a Game of Life by hand, says he could do it only by using plenty of comments and white space [5].
The traps are the ones from What a program is: punctuation in a comment is carried out, and a bracket in a comment can break the pairing. Either keep those characters out of comments, or put a long comment inside a loop that cannot run.
For publishing, many authors strip a program to its commands and wrap the lines. That is why famous programs look like walls of symbols. They were not written that way.
Finding mistakes
- Step through it. Run the program one command at a time and watch the tape. The playground does this.
- Print the tape. Müller's own interpreter of 1993 had a ninth command for this: a
#in the program prints the first ten cells [6]. Some implementations still have it [7]. - Test one piece at a time, with the cells it needs set by hand.
- Use a strict interpreter. One that stops when the pointer leaves the tape, or when a cell goes below zero, finds mistakes that a forgiving one hides. brainfuck.org offers one that was made for contests and is described there as restrictive [8].
Letting a program write the program
Beyond a few thousand commands, most brainfuck is generated. The author writes in something more comfortable, and a tool turns that into the eight commands. There are four ways, in the table below.
| Way | What you write | Examples |
|---|---|---|
| Macros | Brainfuck with names: "add a to b", "while x". Each name expands to a fixed piece of brainfuck | Erik Bosman's Mandelbrot viewer, built from a set of macro files [9]. The Esolang wiki says they were expanded with the C preprocessor [3] |
| A compiler from a small language | A simple language with variables and loops, designed to be turned into brainfuck | BFBASIC by Jeffry Johnston (2003), from a kind of BASIC [10]. A recent ray tracer was written in a small language of its author's own [11] |
| A compiler from a real language | Ordinary C | C2BF by Gregor Richards, unfinished [12]. ELVM by shinh, which compiles C to a simple intermediate form and from there to brainfuck [13] |
| A generator for one job | A program in any language that prints the brainfuck for one specific task | The chat models of this site: a JavaScript program wrote them [14]. lobotoml, which compiles a small neural network [15] |
How does C fit on a tape? C2BF's description gives the idea: the tape is cut into equal chunks of five cells, some cells of each chunk belong to a stack and some to a heap, and a function call becomes a round of one big loop that picks the next block to run [12]. Programs made this way are long and slow, and they work. With ELVM, the C compiler itself has been compiled to brainfuck [13].
One more kind of tool searches. The shortest known "Hello, World!" was found by a program that tried out short brainfuck programs systematically [16].
Writing for speed
A fast implementation does not carry out commands one by one. It recognises common shapes, above all the loops among the idioms, and does each in one operation [17]. So a program is fast when its heavy work has those shapes:
- Loops that end where they begin. A loop whose body moves the pointer and brings it back, and takes exactly one off the starting cell each round, is a multiplication. It costs one step however large the number is.
- No loops inside the hot loop. A loop that holds another loop cannot be merged in this way.
- Short walks. Keep data that is used together close together.
The chat programs of this site were designed around this. Their arithmetic is built so that the expensive part consists of loops of the simple shape; that is what brought one division from more than a million steps to about a hundred measured [14]. How implementations do the merging is on the page Implementations.
Limits
- This is an overview. The exact programs for comparing, dividing and printing numbers are in the patterns Less than, Divide, with remainder and Print a number in decimal, and in the cookbook we cite.
- We did not run the tools in the table of four ways ourselves, except this site's own generator. What they do is taken from their authors' descriptions.
Read next
Notes and sources
Where a source is a community wiki or the author's own account, the entry or the text says so.
- Daniel B Cristofani, "get good at brainfuck": the series on brainfuck.org. Opened 11 October 2026.
- Esolang wiki, "Brainfuck algorithms": the wiki's collection of algorithms. Opened 11 October 2026.
- Esolang wiki, "Brainfuck code generation": the wiki's list of tools that write brainfuck. Opened 11 October 2026.
- Esolang wiki, "Brainfuck constants": the wiki's table of constants. Opened 11 October 2026.
- Linus Åkesson, "Game of Life in Brainfuck": the page on linusakesson.net. Opened 11 October 2026.
- Urban Müller, brainfuck-2.lha, Aminet, dev/lang, dated 9 June 1993: the package page on Aminet. We read the files in the archive:
README,bfc.asm(the compiler),bfi.c(the interpreter) and the six example programs. Sizes and dates are those of the archive listing. Opened 11 October 2026. - Daniel B Cristofani, "The Epistle to the Implementors": the epistle on brainfuck.org. Opened 11 October 2026.
- Daniel B Cristofani, brainfuck.org: the main page. The page licenses his programs under Creative Commons Attribution-ShareAlike 4.0. Opened 11 October 2026.
- Erik Bosman, Mandelbrot set viewer, files dated 1 June 2003, in the Brainfuck Archive: the mandelbrot directory with its
README.txt. Opened 11 October 2026. - Esolang wiki, "BFBASIC": the wiki article on BFBASIC. Opened 11 October 2026.
- "Writing a ray tracer in Brainfuck", 24 September 2026: the article on epestr.com. All figures are the author's own. Opened 11 October 2026.
- Esolang wiki, "C2BF": the wiki article on C2BF. Opened 11 October 2026.
- shinh, ELVM Compiler Infrastructure, README: shinh/elvm on GitHub. Opened 11 October 2026.
- This site's own project. Counts and measurements are on How it works: the numbers and How do I know it is real?; the earlier work we know of is under Has this been done before?. Opened 11 October 2026.
- Ad1th, lobotoml: Ad1th/lobotoml on GitHub. All figures are from its README. Opened 11 October 2026.
- Code Golf Stack Exchange, answers to the "Hello, World!" challenge: the answer by KSab (27 April 2018, 72 bytes) and the answer by primo (3 January 2016, 78 bytes). Read through the Stack Exchange API. Opened 11 October 2026.
- Mats Linander, "brainfuck optimization strategies", 7 January 2015: the article on calmerthanyouare.org. Opened 11 October 2026.