Learn to Code via Tutorials on Repl.it!

← Back to all posts
BrainF - A Complete Guide

BrainF Tutorial

So, I bet you've all seen in BrainF in Repl.it's list of languages and wondered, "what on Earth's this?" You clicked on it, and opened up the examples. There's a load of horribly confusing-looking code, using only 8 characters. Well, here's a tutorial to teach you how to use the programming language which is true to its name.

1. How the language works

BrainF works using memory cells. Repl.it's BrainF IDE, like most, has 30,000 memory cells, each of which can hold a single byte (some have fewer, most commonly mobile apps, like BrainFudge for Android which only has 300). There is a pointer, which always selects a single cell. The language works by moving the pointer and changing the value of the current cell - you can only ever handle the cell that is selected. When the program starts, every cell holds 0 and the pointer points to cell 0.

2. The characters

As I mentioned earlier, BrainF uses only 8 characers: > < + - [ ] , .

  • > <
    • Move the pointer right and left
  • + -
    • Increment and decrement the value of the current cell. If the value is lowered below 0 or raised above 127, the program will still function and the cells will retain their values, but they will not be printable.
  • .
    • Take the value of the current cell, and print the ASCII character with that value
  • ,
    • Take an input of a single character, and store that character's ASCII value to the current cell, overwriting any existing data in that cell. If more than one character is provided, the Repl.it IDE will ignore any after the first.
  • [ ]
    • This is where it gets more complicated: these two characters are what allow you to create a loop in BrainF.
    • When [ is reached, if the value of the current cell is 0, the program will skip to the corresponding ]; if the value of the current cell is not 0, the program will keep running
    • When ] is reached, if the value of the current cell is 0, the program will keep running. If the value of the current cell is not 0, the program will jump back to the corresponding [.
    • This means that [ ] is effectively while current_cell != 0. There are, however, 2 main things to be careful of when creating loops:
      • 99 times out of 100, you want to finish an iteration of the loop with the pointer at the same place as it started
      • You also need to make sure the cell in question is actually decreasing towards 0: the program doesn't do this automatically, you need to make sure you do it, otherwise you will end up with an infinite loop, which most BrainF IDEs don't like.

3. Techniques

There are a few little handy hints you might find useful when actually writing programs in BrainF:

  • Say you want to print the character H, which has an ASCII value of 72. Instead of doing +++...+++. with 72 +s, you can do something like this:
    What this does:
    • The > at the beginning moves the pointer to the second cell
    • 8 +s puts a value of 8 into the second cell
    • We then enter into a loop: < takes the pointer back to the first cell, we then add 9 to the first cell, before going back to the second cell (>) and subtracting one (-). This loop means we add 9 to the first cell a total of 8 times, resulting in a total value of 72.
    • We then move the pointer back to the first cell (<) and print (.) H, because H has an ASCII value of 72.
  • [-] sets the current cell's value to 0

Here's an example BrainF program, with comments!

4. Upvoting this tutorial

It is a strange but true scientific fact that if you upvote this tutorial, not only will you receive vee freebucks, but you will immediately find BrainF easier. All seriousness aside though, please do upvote if you enjoyed this tutorial or found it useful.

Coming (not very) soon: How to write a BrainF interpreter in Python!

One final tip: don't take my example, never put comments in your BrainF code. If you do comment it, you'll be able to come back in a month's time and actually understand your code. If this happened, the language would need renaming:


Oh my goodness, I didn't even realize you could do multiplication like that! That's awesome.


The tutorial is excellent. (wondering if the example on paragraph 3 is missing a leading greater-than sign).

BrainF is mind blowing (for me anyway!) Can a practical use be presented please. Thanks!


@larryk thx for that correction, I'll fix it right away.
BrainF has exactly zero practical use - that's one of the reasons that I personally like it so much!


@minx28 Actually, a practical use might be for those who wish to learn Assembly language! BrainF is the easy way to do it. After understanding the principles, then they can learn to do it with Assembly! :) Still blown away - Have a great day!


I am confusion


@rediar any particular section you don't understand?


@minx28 Its harder to understand for people who dont know memory very well. Programming languages like python dont really tell you that anymore because it is high level


@AustinBPark Yeah... I semi regret choosing python as my first language.


@rediar no its a good first language. it was mine as well. I went on to learn about reverse engineering and game hacking in C++. python gives you good knowledge about programming. Try using c++ to interact with your machine though.


@AustinBPark what about rust? I heard it was similar to cpp and more modern, it is similar?


@rediar I really think you should get into C++. I have heard of rust as well, but C++ is more well known, has existed for longer, more machines have it, and there is more stuff made in it. so you will be able to find libraries more often in C++. ALso when you try to get something like a game engine for example, the language used on them would be C++ often. so that would be a few reasons you should learn C++. Your choice though. I dont know anything about rust



For anyone new looking at this necropost, I would actually recommend Rust.

First, I will counterargument @AustinBPark's claims

so you will be able to find libraries more often in C++
Even though C++ has more frameworks, which I will not dispute, C++ has no official package manager, while Rust has cargo (58,740 packages, 6,459,506,585 downloads overall).
more machines have it
Both Rust and C++ code are compiled languages, meaning they turn into binary files your machine can execute. More computers might come with the C++ compiler built in, but all machines can run this binary code, and shipping this software (Rust or C++) doesn't even require the end user to install anything, nullifying this claim.

like a game engine for example, the language used on them would be C++ often.

C++ is used in Unreal engine (blueprints is a better graphical alternative to C++ for Unreal though)

The thing about this is that Unreal engine's domain is TINY compared to Unity, which uses C#. If you want to get into game dev, you'll be much more successful with C#.

C++ is more well known, has existed for longer

That may be true, but how does that make a language better than another?

Just because BrainF*** was created 2 years before Javascript doesn't make it a better language.
And all the names we're familiar with today (C++, Javascript, Amazon, etc) started small, and this gives Rust more potential than any language ever could. Newer languages tend to be better designed than older languages too, since they had time to improve from their ancestor's mistakes.

Now, time for my own argument.

  • Rust has better + safer memory management due to the fact it uses ownership instead of allocation/deallocation.

  • The compiler is your best friend. It is like a strict teacher, fussing about everything it can find. It encourages good coding behaviors, and makes variables immutable by default, essentially encouraging functional programming.

  • Personally, Rust looks cleaner than C++ in most aspects. Here are two identical pieces of code in both languages:



No wonder Rust has been the most loved programming language for 5 years in a row, while C++ took 18th most loved language (8th most dreaded language)


Oh, i know why it is called BrainF, because it looks like a mess but when i learn it it just looks easy.


@RonAstern aka f-up your brain


Hey guys, BrainF can use ; comments


hey can i have as bot




Vee freebucks


where are my vee v-bucks >:(

<!-- hi, this is my first day at the 5 day challenging, wish me luck!-->

@JazzIt good luck lol






I wonder why +[[>+>+<<-]>+>[<<+>>-]<<.>] does what it does instead of going 1 2 3 4 5 6 7 8 9 10 11...


@LoganB2 BrainF prints unicode chars, not numbers
also, it prints one char at a time
I can write you a program that writes 0123456789…


@LoganB2 Your code prints all unicode characters in order. Thank you!