The language consists of eight
commands. A Brainfuck program is a sequence of these commands. The commands are executed sequentially, with some exceptions: an
instruction pointer begins at the first command, and each command it points to is executed, after which it normally moves forward to the next command. The program terminates when the instruction pointer moves past the last command. In the canonical abstract machine model for Brainfuck, a program operates on a
one-dimensional tape of memory cells indexed from zero and
unbounded to the right. Each cell contains an
8-bit unsigned integer (0–255) and is initialized to zero. Arithmetic on cells
wraps modulo 256, so incrementing 255 produces 0 and decrementing 0 produces 255. A
data pointer (initialized to point to the leftmost
byte of the array) indicates the current cell on the tape and can move left or right. Moving left from the first cell (index 0) is undefined. The language provides eight commands that modify the tape, move the pointer, perform input/output (using the
ASCII character encoding) or control loops. The eight language commands each consist of a single character:
Notes • The much older language
P′′ already featured three of these commands, >, [, and ], denoting them R, (, and ) instead, while the fourth P′′ command, denoted λ, combined + and [ … ] can be nested arbitrarily and all brackets must be properly matched, making the set of valid programs a
context-free language. Many practical implementations allocate a finite array of cells (commonly at least 30 000 bytes) and use 8‑bit cells with wraparound, although neither the exact cell count nor wraparound behavior is required by all interpreters. • In implementations with 8-bit unsigned cells and arithmetic wrapping at 256, one of the + or - instructions is redundant. Incrementing a cell by one is equivalent to decrementing it 255 times in succession, and decrementing by one is equivalent to incrementing it 255 times in succession. Accordingly, either the + instruction may be expressed as -^{255}, or the - instruction as +^{255}.{{Efn|H^{k} \; \rightarrow \; \underbrace{H H \dots H}_{k \text{ times}} defines repeated application of an instruction.}} Brainfuck programs are usually difficult to comprehend. This is partly because any mildly complex task requires a long sequence of commands and partly because the program's text gives no direct indications of the program's
state. These, as well as Brainfuck's inefficiency and its limited input/output capabilities, are some of the reasons it is not used for serious programming. Nonetheless, like any Turing-complete language, Brainfuck is theoretically capable of computing any
computable function or simulating any other
computational model if given access to an unlimited amount of memory and time. A variety of Brainfuck programs have been written. Although Brainfuck programs, especially complicated ones, are difficult to write, it is quite trivial to write an interpreter for Brainfuck in a more typical language such as C due to its simplicity. Brainfuck interpreters
written in the Brainfuck language itself also exist. == Examples ==