I have a basic program in C that I wish to convert to the language brainfsck but cannot find anything on the subject. I find many "brainfuck to C" converters but not the other way around. I found C2BF but do not understand how to use it or if that's what I need.
-
3I want to actually convert it into source code, not re-compile it in some intermediary step – MyNameIsKhan Jul 10 '12 at 15:56
-
6It's still a compiler, what you're outputting is BF code. Keep in mind that since BF is so much more limited than C (even though it's turing complete) this will be nontrivial, the concept of a "pointer" doesn't really make sense, or example. – Kristopher Micinski Jul 10 '12 at 15:58
-
You'd probably be better off converting assembly to brainfsck, and compiling the C code to assembly. – Claudiu Jul 10 '12 at 15:59
-
2I know how to compile C to assembly, but then how to compile assembly to brainfsck? – MyNameIsKhan Jul 10 '12 at 16:00
-
I did write a program for this once. it is total fun if you have time. Just do the constructs you need for your application. – perreal Jul 10 '12 at 16:03
-
4BTW, C2BF is distributed as source code (since you mentioned that you didn't know what to do with it). You have to compile it yourself in order to use it. Download the source and read the README file and makefile for information about compiling and using it. – bta Jul 10 '12 at 16:40
-
I don't even know how to do that; where's the C2BF file I need? – MyNameIsKhan Jul 10 '12 at 16:53
2 Answers
C2BF is a C program that is distributed as C source code.
To build the C2BF you need svn, Gnu Make and yacc and flex to be installed. After you have compiled C2BF yourself it should do exactly what you expect it to.
If you are using some kind of Linux flavour:
cd ${HOME}
svn co https://c2bf.svn.sourceforge.net/svnroot/brainfuck/c2bf/trunk c2bf
cd c2bf
sudo apt-get install bison flex # or yum install bison flex
YACC=bison make
should give you the compiler in ${HOME}/c2bf/cc/c2bf-cc
.
If you are using Windows you have to install something like MinGW or cygwin to compile the stuff.

- 44,602
- 16
- 137
- 156

- 24,223
- 14
- 73
- 76
I can only recommend using https://brainfuck.antosser.xyz/
It's a website that can compile easy-to-read code to Brainfuck. It's not C, but has a very easy syntax to learn.
For example printing all numbers from 1 to 100 divisible by 3 in the decimal system would be:
var i 3
var newl 10
#while i
printdec i
print newl
add i 3
#if i num 102
set i 0
#end
#end
#end
The compiler is very optimized and produces highly compressed code such as this
>>>>>>>>>+++>++++++++++<[<<<<<<<<<[-]>>[-]>>>>>[-]<<<[-]>>>>>[-<<<+>>>]<<<[->>>+<<<<<<<+>>>>]<<<<<[-
]>[-<+>]<[->>>>>>+[-<+>]<[->+>+<<]<++++++++++>>>[-<<<->>>]<<<[->+<]>[[-]<+>]<-[[-]>>[-]<<<<<+>>>]<<<
<]>[->>>>+<<<<]>>>>>[-<<<<<+>>>>>]<[->+<]>>[-]<[->+<]>[-<<<<+[->>+<<]>>[-<<+<+>>>]<++++++++++<<[->>-
<<]>>[->+<]>[[-]<+>]<-[[-]<[-]>>>+<<]>>>]<[-<+>]<<<[->>>+<<<]>>[-<<+>>]<<[->>+<<]>>[-<<+>>>>+<<]>>[-
<<+>>]<<[->>+<<<+>]<[[-]>++++++++[<<++++++>>-]<<.>]>>[-<+>]<[->+>+<<]>>[-<<+>>]<<[->>+<<<+>]<[[-]>++
++++++[>++++++<-]>.<<]>>>[-]<<++++++++[<<<<++++++>>>>-]<<<<.>>>>>>>>.<+++[-<<<+>>>]<<<[->>>+<<<<<<+>
>>]++++++++++[<++++++++++>-]<++<<[->>-<<]>>[->+<]>[[-]<+>]<-[[-]>>>>[-]<<<<]>>>>]

- 346
- 1
- 9
-
I made the compiler so that you can write some code in 2 minutes and then very easily compile it to Brainfuck. How that snippet works is by making all the operations in binary and the converting them to decimal, and this is in fact very slow. All the inefficiencies you see are because the code is literally only 11 lines of code. You could instead manually make the 3 variables for the decimal places and that code would be very efficient. I call it highly compressed because there's a very smart algorithm that minimizes the pointer movement.Tl;dr: You could write it more efficiently if u wanted t – Antosser Oct 14 '22 at 14:42
-
I wouldn't have called this "highly compressed"; like most mechanically generated brainfuck code, it's around four or five times as long as it needs to be. (Generating decent brainfuck is hard.) Some signs pointing to possible improvements: Zeroing things that are already zero. (A small problem symptomatic of bigger problems.) Going back and forth rather than doing things in the most efficient order. Excessive moving and copying of variables. Near-duplicated code: two loops, over 80 commands each, for "carry from 1s to 10s" and "carry from 10s to 100s"; three copies of "add 48 and output". – Daniel Cristofani Oct 16 '22 at 10:42
-
The main issue isn't that you're keeping i in a cell and then using your "printdec" code to convert it to decimal for output; it's that your printdec code is over 500 bytes when it could be like 100 or 120. – Daniel Cristofani Oct 16 '22 at 10:43
-
@DanielCristofani The point is that it's easy. You can still write more efficient code but you'll sacrifice readability – Antosser Apr 22 '23 at 20:55
-
A more efficient printdec implementation (such as https://stackoverflow.com/a/75543158/5180920 ) wouldn't be less readable, and wouldn't make the line "printdec i" less readable. I get that you wanted to make it easy to generate brainfuck programs. A popular impulse, but I never understood it. If someone isn't interested in the experience of programming in brainfuck, either as a puzzle or to develop skill and make beautiful things, then why not just code in a high-level language and compile it to efficient machine code? Why mess with brainfuck at all if you don't like brainfuck? – Daniel Cristofani Apr 28 '23 at 08:24