Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 978533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:04:21+00:00 2026-05-16T04:04:21+00:00

For the uninitiated, Brainfuck is a Turing-complete language with only 8 commands, all of

  • 0

For the uninitiated, Brainfuck is a Turing-complete language with only 8 commands, all of which have literal equivalents in C:

bf    c
----------------------
>     ++ptr;
<     --ptr;
+     ++*ptr;
-     --*ptr;
.     putchar(*ptr);
,     *ptr=getchar();
[     while (*ptr) {
]     }

On any linux distro that has a package manager, you ought to be able to find and install the package beef, a Brainfuck interpreter so you can play along at home.

As you can see above, Brainfuck has but one control structure, […] which translates to C as:

while (*ptr) { … }

Which gives you all the control of IF VAR = 0 THEN GOTO 10 from BASIC. The following will call getchar() until it returns 0:

,    # *ptr = getchar();
[    # while (*ptr) {
  >,    # *(++ptr) = getchar();
]    # }

But what if I only want to read to a newline char \n? After having some difficulty wrapping my brain around how this could be adapted to work as a simple if I came up with the following:

,            # *ptr = getchar();      /* store input                               */
----------   # *ptr -= 10;            /* test for \n by subtracting 10 before loop */
[            # while (*ptr) {         /* if *ptr == 0, last char read was \n       */
  ++++++++++   # *ptr += 10;          /*   wasn't \n, add 10 back to val under ptr */
  >,           # *(++ptr) = getchar();
  ----------   # *ptr -= 10;
]            # }

(If anyone has a better way of doing this, please let me know)

Now lets say I want to test break out of that loop on \r in addition to \n. How can I test for either, given that I only have one opportunity to break out of the loop? My goal is to be able to emulate switch, nested ifs or if/else ifs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:04:22+00:00Added an answer on May 16, 2026 at 4:04 am

    I didn’t take a look at the link yet, but I imagine that AShelly’s answer is probably more universally useful…however, having not dared to let my fingers type out some Brainfuck before, I decided that now was as good of a time as any and was able to get the following, which I think works.

    The second half is just for printing out what was captured by the first part. Also, I won’t lie, my brain does indeed feel thoroughly “fucked” after having tried to work this out…A very aptly-named language, heh.

    +
    [
        >,
        ----------
        [
            ---
            [
                +++++++++++++
                >>
            ]
        ]
    
        <
        [
          >>
        ]
        <
    ]
    
    ++++++++++++
    [
        <
    ]
    
    >
    [
        .>
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code that has been working for almost 4 years (since boost
I have a c++ program that dies because of out of memory error. Do
I have a basic page, with a div containing text, and a form to
I have a fairly simple problem for someone familiar with js. I quite like
I'm relatively new to Ruby and have limited time therefore I try out simple
I have an application that uses NSDocument to open files. I have an NSView
All the initialized global/static variables will go to initialized data section . All the
I am using xCode for the first time - since 1986 I've used other
What is the best format to communicate an array of strings in one string
Am trying to get myself used to working with dynamic arrays in c++ and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.