Brainf*ck - a programming language

Brainfuck is a minimalist, esoteric programming language created by Urban Müller in 1993. It is known for its simplicity, as the entire language consists of only eight commands. Despite its simplicity, Brainfuck is Turing-complete, meaning it can theoretically solve any computational problem given enough time and memory.

While Brainfuck isn't practical for real-world applications, it is an excellent exercise in understanding the fundamentals of computation, memory management, and instruction handling. Programmers often explore Brainfuck for fun or as a challenge to better understand low-level programming concepts.

The eight Brainfuck commands

Command Description C language equivalent
> Moving the memory pointer to the right. ++p;
< Moving the memory pointer to the left. --p;
+ Incrementing the memory cell at the pointer. ++(*p);
- Decrementing the memory cell at the pointer. --(*p);
. Displaying the character at the memory cell. putchar(*p);
, Inputting a character and storing it in the memory cell. *p=getchar();
[ Starting a loop - executing the instructions inside until the memory cell is zero. while(*p){
] Ending a loop. }

Brainfuck operates on an array of memory cells (usually 30,000), each initialized to zero (char array[30000] = {0};). A pointer starts at the first cell and can move left or right across the array. Commands modify the value at the current memory cell or handle input/output.

The ASCII table

ASCII

"Hello, World!" program

Everything that is not a Brainf*ck command is treated as a comment.


// A loop setting convenient values (near letters in ASCII)

++++++++++ // Cell 0 = 10
[ // While loop (as long as cell 0 is not 0)
>+++++++ // Cell 1 add 7 
>++++++++++ // Cell 2 add 10
>+++ // Cell 3 add 3
>+ // Cell 4 add 1
<<<<- // Return to cell 0 and decrement it
]

// After the loop ends:
// Cell 1 = 70
// Cell 2 = 100
// Cell 3 = 30
// Cell 4 = 10

>++. // Cell 1 (70) add 2 = 72 (in ASCII 'H') = prints
>+. // Cell 2 (100) add 1 = 101 (in ASCII 'e') = prints
+++++++. // Cell 2 (101) add 7 = 108 (in ASCII 'l') = prints
. // prints l
+++. // Cell 2 (108) add 3 = 111 (in ASCII 'o') = prints
>++. // Cell 3 (30) add 2 = 32 (in ASCII ' ') = prints
<<+++++++++++++++. // Cell 1 (72) add 15 = 87 (in ASCII 'W') = prints
>. // Cell 2 (111) (in ASCII 'o') = prints
+++. // Cell 2 (111) add 3 = 114 (in ASCII 'r') = prints
------. // Cell 2 (114) subtract 6 = 108 (in ASCII 'l') = prints
--------. // Cell 2 (114) subtract 6 = 108 (in ASCII 'l') = prints
>+. // Cell 3 (32) add 1 = 33 (in ASCII '!') = prints
>. // Cell 4 (10) (in ASCII '\n') = prints
                                    

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
                                    

You can find a Brainf*ck compiler here.