The eight language commands each consist of a single character:
Command | Meaning |
---|---|
> |
Increment the data pointer (to point to the next cell to the right). |
< |
Decrement the data pointer (to point to the next cell to the left). |
+ |
Increment (increase by one) the byte at the data pointer. |
- |
Decrement (decrease by one) the byte at the data pointer. |
. |
Output the byte at the data pointer. |
, |
Accept one byte of input, storing its value in the byte at the data pointer. |
[ |
If the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching [ command. |
] |
If the byte at the data pointer is nonzero, then instead of moving the instruction pointer back to the next command, jump it forward to the command after the matching ] command. |
]
command may instead be translated as an unconditional jump to the corresponding [
command, or vice versa; programs will behave the same but will run more slowly, due to unnecessary double searching.)
[
and ]
match as parentheses usually do: each [
matches exactly one ]
and vice versa, the [
comes first, and there can be no unmatched [
or ]
between the two.
Brainfuck programs can be translated into C using the following substitutions, assuming ptr is of type char* and has been initialized to point to an array of zeroed bytes:
Command | C equivalent |
---|---|
> |
++ptr; |
< |
--ptr; |
+ |
++*ptr; |
- |
--*ptr; |
. |
putchar(*ptr); |
, |
*ptr = getchar(); |
[ |
while (*ptr) { |
] |
} |