Shortest infinite loop producing no output

  • Your task is to create the shortest infinite loop!



    The point of this challenge is to create an infinite loop producing no output, unlike its possible duplicate. The reason to this is because the code might be shorter if no output is given.



    Rules




    • Each submission must be a full program.

    • You must create the shortest infinite loop.

    • Even if your program runs out of memory eventually, it is still accepted as long as it is running the whole time from the start to when it runs out of memory. Also when it runs out of memory, it should still not print anything to STDERR.

    • The program must take no input (however, reading from a file is allowed), and should not print anything to STDOUT. Output to a file is also forbidden.

    • The program must not write anything to STDERR.

    • Feel free to use a language (or language version) even if it's newer than this challenge.
      -Note that there must be an interpreter so the submission can be tested. It is allowed (and even encouraged) to write this interpreter yourself for a previously unimplemented language. :D

    • Submissions are scored in bytes, in an appropriate (pre-existing) encoding, usually (but not necessarily) UTF-8. Some languages, like Folders, are a bit tricky to score - if in doubt, please ask on Meta.

    • This is not about finding the language with the shortest infinite loop program. This is about finding the shortest infinite loop program in every language. Therefore, I will not accept an answer.

    • If your language of choice is a trivial variant of another (potentially more popular) language which already has an answer (think BASIC or SQL dialects, Unix shells or trivial Brainf**k-derivatives like Alphuck), consider adding a note to the existing answer that the same or a very similar solution is also the shortest in the other language.

    • There should be a website such as Wikipedia, Esolangs, or GitHub for the language. For example, if the language is CJam, then one could link to the site in the header like #[CJam](http://sourceforge.net/p/cjam/wiki/Home/), X bytes.

    • Standard loopholes are not allowed.



    (I have taken some of these rules from Martin Büttner's "Hello World" challenge)






    Please feel free to post in the comments to tell me how this challenge could be improved.



    Catalogue



    This is a Stack Snippet which generates both an alphabetical catalogue of the used languages, and an overall leaderboard. To make sure your answer shows up, please start it with this Markdown header:



    # Language name, X bytes


    Obviously replacing Language name and X bytes with the proper items. If you want to link to the languages' website, use this template, as posted above:



    #[Language name](http://link.to/the/language), X bytes


    Now, finally, here's the snippet: (Try pressing "Full page" for a better view.)





    var QUESTION_ID=59347;var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk";var OVERRIDE_USER=41805;var answers=[],answers_hash,answer_ids,answer_page=1,more_answers=true,comment_page;function answersUrl(index){return"//api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(index,answers){return"//api.stackexchange.com/2.2/answers/"+answers.join(';')+"/comments?page="+index+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){answers.push.apply(answers,data.items);answers_hash=[];answer_ids=[];data.items.forEach(function(a){a.comments=[];var id=+a.share_link.match(/\d+/);answer_ids.push(id);answers_hash[id]=a});if(!data.has_more)more_answers=false;comment_page=1;getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:true,success:function(data){data.items.forEach(function(c){if(c.owner.user_id===OVERRIDE_USER)answers_hash[c.post_id].comments.push(c)});if(data.has_more)getComments();else if(more_answers)getAnswers();else process()}})}getAnswers();var SCORE_REG=/<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;var OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(a){return a.owner.display_name}function process(){var valid=[];answers.forEach(function(a){var body=a.body;a.comments.forEach(function(c){if(OVERRIDE_REG.test(c.body))body='<h1>'+c.body.replace(OVERRIDE_REG,'')+'</h1>'});var match=body.match(SCORE_REG);if(match)valid.push({user:getAuthorName(a),size:+match[2],language:match[1],link:a.share_link,});else console.log(body)});valid.sort(function(a,b){var aB=a.size,bB=b.size;return aB-bB});var languages={};var place=1;var lastSize=null;var lastPlace=1;valid.forEach(function(a){if(a.size!=lastSize)lastPlace=place;lastSize=a.size;++place;var answer=jQuery("#answer-template").html();answer=answer.replace("{{PLACE}}",lastPlace+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link);answer=jQuery(answer);jQuery("#answers").append(answer);var lang=a.language;lang=jQuery('<a>'+lang+'</a>').text();languages[lang]=languages[lang]||{lang:a.language,lang_raw:lang,user:a.user,size:a.size,link:a.link}});var langs=[];for(var lang in languages)if(languages.hasOwnProperty(lang))langs.push(languages[lang]);langs.sort(function(a,b){if(a.lang_raw.toLowerCase()>b.lang_raw.toLowerCase())return 1;if(a.lang_raw.toLowerCase()<b.lang_raw.toLowerCase())return-1;return 0});for(var i=0;i<langs.length;++i){var language=jQuery("#language-template").html();var lang=langs[i];language=language.replace("{{LANGUAGE}}",lang.lang).replace("{{NAME}}",lang.user).replace("{{SIZE}}",lang.size).replace("{{LINK}}",lang.link);language=jQuery(language);jQuery("#languages").append(language)}}

    body{text-align:left!important}#answer-list{padding:10px;width:500px;float:left}#language-list{padding:10px;padding-right:40px;width:500px;float:left}table thead{font-weight:700}table td{padding:5px}

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr> </thead> <tbody id="languages"> </tbody> </table> </div> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr> </thead> <tbody id="answers"> </tbody> </table> </div> <table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr> </tbody> </table>




    I've got to start posting programs with a negative byte count to beat all these empty files!

    This challenge is interesting because it brings out lots of 0 byte languages (some of which are NOT esolangs). FWIW, most declarative languages have an implicit infinite loop because declarative languages don't have loops in their syntax (they assume they're running in an infinite loop). Ladder diagrams are perhaps among the oldest such languages. Then you have the Instruction Language (IL), a sort of assembly for PLCs that also assume an infinite loop. ILs, like assembly are different between manufacturers

    Are programs that read and execute their own source code allowed, or does file I/O break the "must take no input" rule?

    @ThisSuitIsBlackNot Yes, file input is allowed.

    Can you print `""`, an empty string?

    Most C compilers create a temporary file to avoid torturing the memory when resolving preprocessor directives. Does it make this answer non-competing because it throws the compiler itself in an infinite loop using a recursive #inclusion?

    Btw, here's a general note: Open the links on the leaderboard on a new tab/window.

    @OldBunny2800 No, you can't

    @KritixiLithos Do you mean, no trailing newline, right? Because just printing an empty string without a trailing newline is no output at all.

    @EriktheGolfer As weird as it sounds, not even an empty string `""` should be outputted.

    @KritixiLithos I mean, without the quotes and trailing newlines. Of course `"` can't be in the output.

    @EriktheGolfer I also mean without the quotes and without trailing newlines.

    @KritixiLithos Oh... gosh. So, let's say that you can't use `print(end='')` in Python 3, for example. I think this is really weird, but I'll adhere to the rule.

  • Befunge, 0 bytes





    Yup. A Befunge program exists in a two-dimensional playfield with fixed size which wraps around the edges. With nothing in that space to interfere, the program counter runs in an infinite loop by default :)


    Aw, I was gonna post this. :/

    @daniero It may have some relation to this standard loophole.... I personally don't agree with the standard loophole.

    @Justin That loophole only applies to certain types of challenges.

    You can use `

     
    ` for an empty-looking code block.

    First thing I looked for

  • L00P, 0 bytes





    This lang was made for looping, and that's just what it'll do...


    LOL. +1 for choice of language.

    ... one of these days this lang is gonna loop all over you

    +1 for the musical reference, and congrats on another gold badge!

    The best reference I have seen

  • C64 Machine Code, 2 Bytes


    D0 FE

    Branches to itself if the zero flag is not set.


    Branches are single-byte offsets from the next instruction location, and 254 is -2 in two's complement... the BNE instruction (D0) takes one byte of memory, and the offset takes a second byte, so branching two bytes back branches back to itself. The zero flag is always cleared when code is loaded into memory.


    Note that this is not a recursive subroutine call, so you will never run out of memory. Also note that there is no header, compiler, or executable overhead... it is truly a two-byte program :)


    Or EB FE for the unconditional jump

    Shouldn't this work on any machine with a 6502 / 6510 family processor, not just a C64? Also, what you've written is machine code. The assembly would be `BNE -2`

    +1 for making an actual program that is small, instead of trying to find the most obscure language that just so happens to have the least characters to represent the structure.

    @user45891 EB FE is x86. 6502/6510 doesn't have an unconditional short jump instruction.

    Ya, what Random832 said... I had thought there might be an unconditional branch, but when I checked my (old, old, cracked-plastic-binder) reference manual, there was not. EB was "reserved for future expansion", though, so maybe that was added in to a later version of the chip.

    Steveverrill, touché, it is machine code, indeed. And yes, I thought more people would recognize the Commodore 64 than the 65xx family in general :) The VIC-20 used the 6502 and would have been able to run this. So, technically, would my 1541 floppy drive... I vaguely recall being able to reprogram the controller on that. Ah, I still miss my C64 :)

    It wasn't showing up, thought maybe I misunderstood what I was supposed to do with that snippet :) Already removed, and I'm showing up now... though the format of the results is terrible in both fx and ie

    It takes a minute after it's submitted to show up/update. I've changed the formatting slightly; hopefully it's better now. Also, if you want to have an actual strikethrough through the word "Assembly", you can just use `Assembly`.

    @Random832: Actually some of the later 6502 descendants did have an unconditional branch instruction (BRA, 0x80) - see for example this page (search for "BRA").

    @steveverrill This is machine code for any 6502 machine that guarantees carry is clear at program start. The C64 OS specifies this, but machines that leave this unspecified need an additional CLC opcode ($18) at start for a 1-byte penalty.

    Back in the day, a particularly nasty "trick" (imho vandalism) that customers would play on unsuspecting computer store owners was to place two bytes akin to this but for the X86, at the beginning of the bootloader, using DOS' `debug`. This would effectively brick the machine, and most shop staff lacked the nous to know it wasn't just a dead drive.

  • Brainfuck, 3 bytes



    +[]


    Never decrement: never end.


    It's funny when BF beats most other answers.

    I had to think of this when I saw the question on SE's start page.

    My first thought was BF! :D It ended shorter than I thought.

    Alternative, still same bytes: `-[]`

    I bet there's an interpreter somewhere where `]` would work

    @12Me21 The most recent one I wrote, I just realized.

  • ///, 3 bytes



    ///


    Any bonus points for using the language's name as source code?


    I'm honestly surprised that the link actually works, seeing as how it ends with `///` ;)

    @ETHproductions That is rather surprising, but if one thinks about it a bit more, it makes some sense; it's just the URL `esolangs.org` `wiki` `_` `_` `_` `_`

    @AlexL. Not entirely. The web server sees a GET request to `/wiki////`. While that *intended* to be a path, the server can do with that information whatever it wants.

  • Java, 53 bytes


    class A{public static void main(String[]a){for(;;);}}

    Yay full program requirement!


    God is that verbose...

    Aah, I when I received a notification saying that someone posted a Java answer, I guessed the person to be you!

    @minxomat Yep, 45 bytes for an empty `main` :(

    @minxomat gotta love that OOP

    There was a time you could abuse an enum for that - the complaint about the missing main method would occur only *after* instantiation - but Java 8 checks this now beforehand. That's very sad for code golf.

    @Landei Java 7 won't let you either; I think 6 was the last. Either way, I believe that prints to stderr when run, so it wouldn't be valid here.

    @Geobits: It would write the error after the loop - so in our case never...

    That ain't OO. Putting code in a class doesn't make it OO any more than putting a rock in the microwave makes it food.

    @Geobits that guy died tragically, remember?

    Why not using a static block? Works normally...

    Replace `class` with `interface` and drop the `public`. Also, call `main` recursively instead of making a loop.

    @Phoenix `main(a);` == `for(;;);`

    Wouldn't calling it recursively overflow itself and write to STDERR anyway? I assumed this was not allowed. I haven't tried, so maybe it optimizes it away, but if it's no shorter I won't bother.

    @mınxomaτ: Complaining Java is verbose for code-golding is like complaining Jelly is useless for business applications...

  • Prolog, 5 bytes


    a:-a.

    In order to know if predicate a is true, you only need to check if predicate a is true.


    You need to load the file and execute a, both with a command-line arguments, eg. swipl -g a a.pl with Swi-Prolog. Note that the recursion is likely to be optimized as an infinite loop and shouldn't blow the stack.


    Also, this looks like a smiley, but I am not sure how to call it. The dot looks like saliva, so maybe "vegetative state", or "Infiurated programmer with curly hair". Suggestions are welcome.




    Edit: I look at the rules again and for a full program and no command line arguments you need to add this, as user @radrow pointed out, for a total of 10 bytes.


    a:-a.
    :-a.

    Alternatively, for 14 bytes you can have something more poetic:


    :-repeat,fail.

    Check if `a` is true by checking if `a` is true by checking if `a` is true by checking...yay recursion!

    You miss :-a. in your file, it won't magically execute itself

    @radrow with swi-prolog for example you start the program with `swipl -g a a.pl`

    Oh, didn't know that

    @radrow I still edited the answer because using command line arguments might be not alright with the rule, I am not sure, thank you

    Is the newline important though?

    @radrow I tested to be sure, it's either a newline or a space

  • Haskell, 9 bytes


    Infinite recursion of the main function. Should get compiled to a loop due to tail recursion optimization.


    main=main

    It compiles, but if run the runtime system detects the loop and throws the `<>` exception - at least with `ghc`. Maybe some other compiler behaves differently.

    Doing `runhaskell Loop.hs` happily executed it for several minutes on my machine. So, it's at least runnable by an interpreter. I think the `<>` runtime exception that `ghc` throws is purely an implementation detail of the runtime and not part of the Haskell language as specified in any of the Haskell Reports.

    It's an edge case. `runhaskell` happily accepts the code but it doesn't loop. It does nothing. However, the challange only requires to create the loop, not to execute it, so I guess it's fine. Have a +1.

    @GrantS: the Haskell language has no such notion as a “loop”; definitions like `main = main` are semantically in the same bucket as `undefined` or `error "<>"`: bottom values ⟂.

    I think the shortest working haskell loop would be: `main=main>>main`

    @nimi, what do you mean by "it doesn't loop"? It'll run forever with no output.

    @dfeuer: if you start the program with `runhaskell`, yes, it does not terminate but it also uses no cpu time, so it does nothing. Running through a (empty) loop would require at least some cpu activity.

    @nimi, ah, I see what you mean. I wonder what Hugs does with it.

  • Python, 9 bytes



    Works in both 2 and 3.



    while 1:0


    Shortened by @FryAmTheEggman


    note, this does produce output when ran in the Python interpreter

    @Chris_Rands Only in the REPL.

  • x86 ELF executable, 45 bytes


    Unlike the vast majority of these answers, this is a truly complete program, as in a free-standing executable program.


    00000000: 7f45 4c46 0100 0000 0000 0000 0000 0100  .ELF............
    00000010: 0200 0300 2000 0100 2000 0100 0400 0000 .... ... .......
    00000020: ebfe 31c0 40cd 8000 3400 2000 01 [email protected] ..

    The guts of the program are at byte 0x20 ebfe, which is featured in another answer as the smallest NASM program. If you assemble that with NASM however, you get an executable with thousands of un-needed bytes. We can get rid of most of them using the technique outlined here. You may note that this program isn't even as big as the ELF header! This bit of executable golfing malforms the ELF header and program header so they can occupy the same bytes in the file and inserts our program into some unused bytes within the header. Linux will still happily read the header and start execution at offset 0x20 where it spins forever.


    `.com` on DOS would be much shorter :)

    You only get "thousands" of bytes if you let gcc include the startup files. `yasm && ld` makes executables only about 1k.

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used