https://codegolf.stackexchange.com/questions/215705/hello-world-in-zero-lines-of-code Stack Exchange Network Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange [ ] Loading... 1. 2. 0 3. +0 4. + Tour Start here for a quick overview of the site + Help Center Detailed answers to any questions you might have + Meta Discuss the workings and policies of this site + About Us Learn more about Stack Overflow the company + Business Learn more about hiring developers or posting ads with us 5. 6. Log in Sign up 7. current community + Code Golf help chat + Code Golf Meta your communities Sign up or log in to customize your list. more stack exchange communities company blog By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It only takes a minute to sign up. Sign up to join this community [ano] Anybody can ask a question [ano] Anybody can answer [an] The best answers are voted up and rise to the top Code Golf 1. Home 2. 1. Questions 2. Tags 3. Users 4. Unanswered 5. Jobs "Hello, World!" in zero lines of code Ask Question Asked 1 month ago Active 17 days ago Viewed 53k times 105 9 \$\begingroup\$ NPM's sloc is a moderately popular tool for counting source lines of code in a file. The tool will attempt to strip out both single and multiline comments and count the remaining lines in order to get an estimate of the 'true' number of lines of code. However, the parser used is based on regular expressions and is quite simple, so it can be tricked. In this challenge, you will need to construct a hello world program that sloc counts as zero lines of code. The challenge 1. Download sloc. If you have npm, this can be done via npm i sloc. We will use the latest version as of the time of posting, which is 0.2.1. Or, use the online testing environment linked at the bottom of this post. 2. Select any language that sloc supports. A full list is available here. 3. Write a full program in that language that prints the exact string Hello, World! to stdout. The output may optionally have a trailing newline, but no other format is accepted. The program may output anything to stderr. However, to be considered a valid submission, sloc must see the program as zero lines of source code. You can verify this by running sloc myprogram.ext, which should read Source : 0. Example The following Python program is a valid submission: '''"""''';print("Hello, World!");"""'''""" If I run sloc hello.py, I get: ---------- Result ------------ Physical : 1 Source : 0 Comment : 1 Single-line comment : 0 Block comment : 1 Mixed : 0 Empty block comment : 0 Empty : 0 To Do : 0 Number of files read : 1 ---------------------------- Why? Well, sloc uses ''' or """ to determine if something is a mulitline comment in Python. However, it doesn't pair them up correctly. Thus Python parses our program as: '''"""''';print("Hello, World!");"""'''""" \-------/ \--------------------/ \-------/ comment active code comment But sloc parses it as: '''"""''';print("Hello, World!");"""'''""" \----/\----------------------------/\----/ comment comment comment (This is not the shortest valid Python submission, so it shouldn't stop you looking for shorter ones) Scoring The shortest program in bytes wins. A note on feasibility It might be a reasonable question to ask whether this is possible for all languages. I don't know, but all the ones I've looked at so far (Python, C, HTML) have at least one solution. So there's a very good chance it is possible in your language. There are a number of ideas that can work across multiple languages. Online test environment Try it online! You need to: 1. Type your code into the input box 2. Type the extension corresponding to your language into the command args (important!) code-golf restricted-source hello-world share | improve this question | follow | edited Nov 28 '20 at 19:30 [a00] Community 1 asked Nov 28 '20 at 6:13 [XEg] SisyphusSisyphus 8,00033 gold badges2222 silver badges5050 bronze badges \$\endgroup\$ * 8 \$\begingroup\$ sloc recognizes this correctly as two lines, I give up :/ \$\endgroup\$ - ovs Nov 28 '20 at 9:16 * 5 \$\begingroup\$ I will point out that our definition of what counts as a programming language is entirely vestigial, and challenges are not restricted to only "programming languages". They used to be but not anymore. You are of course free to override the defaults. I just don't see a good reason why you are in this case, and thought maybe you didn't know about the new consensus (you wouldn't be the first). \$\endgroup\$ - Wheat Wizard Nov 28 '20 at 9:22 * 5 \$\begingroup\$ Is there a way to run sloc online, rather than needing to install it locally? \$\endgroup\$ - Dominic van Essen Nov 28 '20 at 10:33 * 4 \$\begingroup\$ @WheatWizard I was not aware of the new consensus. Is there some way the old consensus can be marked with a warning? \$\endgroup\$ - Sisyphus Nov 28 '20 at 11:20 * 5 \$\begingroup\$ @DominicvanEssen Despite not knowing coffeescript I've managed to stumble my way into creating a TIO-based online checker. It's now in the last section of the post. \$\endgroup\$ - Sisyphus Nov 28 '20 at 11:36 | show 9 more comments 19 Answers 19 Active Oldest Votes 36 \$\begingroup\$ Haskell, [S:36:S] 35 bytes {-{--}--}main=putStr"Hello, world!" Try it online! Test sloc Since sloc is regex based it is a very good guess to think it cannot identify nested comments. And if we take a look we see that indeed it cannot. If we nest two comments as so: {-{--}-} sloc identifies one line of source and one line of comment. Based on these observations my best guess is that the regex it is using is (\{-((?!-\}).)*-\}|--[^\n]*) If we pop this into a regex engine we will see it matches the nested comment as so: code ^^ {-{--}-} ^^^^^^ comment Ok so we broke it, however this has the opposite of the intended effect. We want sloc to think our code is a comment rather than our comment is code. So we add a "line comment marker" to the part of the comment sloc thinks is code, so that sloc thinks a line comment is being started. {-{--}--} So to sloc there are two comments the block comment {-{--} and the line comment --} but to haskell which actually parses comments, it is all one block comment. So then we just add our code after. Sloc thinks this is still a comment and Haskell knows it is not. share | improve this answer | follow | edited Nov 29 '20 at 22:01 answered Nov 28 '20 at 19:36 [oYG] Wheat WizardWheat Wizard 62.5k1313 gold badges202202 silver badges438438 bronze badges \$\endgroup\$ * 4 \$\begingroup\$ You only need two - between the}s. \$\endgroup\$ - Nitrodon Nov 28 '20 at 21:20 * 1 \$\begingroup\$ Interestingly, my tool of choice cloc is also fooled by your code, unlike most of the other answers! \$\ endgroup\$ - Vorac Dec 11 '20 at 10:45 add a comment | 28 \$\begingroup\$ Java 6, [S:83:S] [S:68:S] 54 Bytes The code : /**/enum C{C;{System.out.print("Hello, World!");}}/**/ Although crashes immediately with error sent to stderr, prints the correct output. Thanks to @Kevin Cruijssen for the tip about removing the System.exit(0). And the result : ---------- Result ------------ Physical : 1 Source : 0 Comment : 1 Single-line comment : 0 Block comment : 1 Mixed : 0 Empty block comment : 0 Empty : 0 To Do : 0 Number of files read : 1 ---------------------------- Thanks @Razetime ! --------------------------------------------------------------------- My initial answer was in java 7+ with 83 Bytes (there's a comma missing) : /**/interface C{static void main(String[]a){System.out.print("Hello World!");}}/**/ share | improve this answer | follow | edited Dec 1 '20 at 13:24 answered Nov 28 '20 at 7:21 [wja] Amir MAmir M 38122 silver badges66 bronze badges \$\endgroup\$ * 4 \$\begingroup\$ this answer may be of some help. \$\endgroup\$ - Razetime Nov 28 '20 at 7:35 * 1 \$\begingroup\$ @Razetime Thanks. I do not have comment upvote privileges yet. \$\endgroup\$ - Amir M Nov 28 '20 at 7:38 * 4 \$\begingroup\$ Very nice! This appears to be a generic approach that covers any language with /* and */ multi line comments. \$\ endgroup\$ - Sisyphus Nov 28 '20 at 8:36 * 6 \$\begingroup\$ An alternate solution for the same length is to prefix your code with //\u000a. \$\endgroup\$ - Sisyphus Nov 28 '20 at 23:57 * 1 \$\begingroup\$ In the third rule it states "The program may output anything to stderr.", so you can removed the System.exit (0); in your Java 6 answer. It will first output the "Hello, World!", and then print an exception to STDERR. PS: You forgot the comma in the string "Hello, World!" in both of your programs. \$\endgroup\$ - Kevin Cruijssen Dec 1 '20 at 10:24 | show 2 more comments 27 \$\begingroup\$ Groovy, 28 bytes /**/print"Hello, World!"/**/ Try it online! --------------------------------------------------------------------- Squirrel, 30 bytes /**/print("Hello, World!")/**/ Try it online! --------------------------------------------------------------------- Swift, 30 bytes /**/print("Hello, World!")/**/ Try it online! --------------------------------------------------------------------- JavaScript (Node.js), 36 bytes /**/console.log("Hello, World!")/**/ As a bonus, this also breaks SE's syntax highlighter. Try it online! --------------------------------------------------------------------- C (gcc), 38 bytes /**/main(){puts("Hello, World!");}/**/ Try it online! --------------------------------------------------------------------- Dart, 39 bytes /**/main(){print("Hello, World!");}/**/ Try it online! --------------------------------------------------------------------- Rust, 42 bytes /**/fn main(){print!("Hello, World!")}/**/ Try it online! --------------------------------------------------------------------- C++ (gcc), 48 bytes /**/main(){__builtin_puts("Hello, World!");}/**/ Try it online! --------------------------------------------------------------------- Scala, 52 bytes /**/object H extends App{print("Hello, World!")}/**/ Try it online! --------------------------------------------------------------------- Go, 69 bytes /**/package main import."fmt" func main(){Print("Hello, World!")}/**/ Try it online! --------------------------------------------------------------------- C# (.NET Core), 75 bytes /**/class P{static void Main(){System.Console.Write("Hello, World!");}}/**/ Try it online! share | improve this answer | follow | edited Nov 29 '20 at 21:18 [c06] hirse 10344 bronze badges answered Nov 28 '20 at 8:26 [c39] Mukundan314Mukundan314 5,81211 gold badge44 silver badges4242 bronze badges \$\endgroup\$ * 12 \$\begingroup\$ With .NET 5 C# you can omit the class declaration and the main method and just write System.Console.Write("Hello world!"); (46 bytes) as a valid code. Sadly, it does not seem to be an option available in tio.run yet... :) \$\endgroup\$ - mishan Nov 28 '20 at 14:48 * 12 \$\begingroup\$ It's funny that the JS code also tricks SE's syntax highlighter. \$\endgroup\$ - Jan Dorniak Nov 28 '20 at 15:44 * 1 \$\begingroup\$ Can the C version end with // rather than /**/? \ $\endgroup\$ - chux - Reinstate Monica Nov 28 '20 at 21:57 * 4 \$\begingroup\$ Apparently the app uses a different syntax highlighter that handles it correctly. \$\endgroup\$ - wastl Nov 28 '20 at 21:59 * 1 \$\begingroup\$ @wastl the first time the app has ever had something work better! \$\endgroup\$ - Tim Nov 28 '20 at 23:48 | show 5 more comments 24 \$\begingroup\$ PHP, 33 bytes Dewi Morgan claimed there was no possible solution, so that obviously made me look further into a way to do it :) (he then followed up with multiple 'shenanigans') Thing is, we can simply do: //Hello, World! The only downside (and what made me initially discard it) is that you must be using a SAPI that has buffering enabled by default. So, if you tried to run it with php cli, it will probably output the // and complain that there was no buffer to delete. But if ran from a web server (even built-in php -S), it will produce the expected Hello World! sloc agrees that has no code: ---------- Result ------------ Physical : 1 Source : 0 Comment : 1 Single-line comment : 1 Block comment : 0 Mixed : 0 Empty block comment : 0 Empty : 0 To Do : 0 Number of files read : 1 ---------------------------- share | improve this answer | follow | edited Dec 1 '20 at 23:11 answered Nov 30 '20 at 2:47 [87f] AngelAngel 52622 silver badges77 bronze badges \$\endgroup\$ * 1 \$\begingroup\$ This is really, really clever. I like it! \$\ endgroup\$ - Sisyphus Nov 30 '20 at 2:53 * 1 \$\begingroup\$ Woops, fixed @KevinCruijssen \$\endgroup\$ - Angel Dec 1 '20 at 23:11 * \$\begingroup\$ That's fire! Well done. \$\endgroup\$ - tfont Dec 9 '20 at 16:17 * \$\begingroup\$ If you allow for differing PHP configuration you can make sure short_open_tags is enabled (isn't that even default?) and then replace is output. This is the bug I exploited. share | improve this answer | follow | edited Nov 29 '20 at 4:59 answered Nov 29 '20 at 4:20 [a8f] Dewi MorganDewi Morgan 42133 silver badges77 bronze badges \$\endgroup\$ * 2 \$\begingroup\$ To get a legal PHP solution, one could consider using the -r flag \$\endgroup\$ - Sisyphus Nov 29 '20 at 4:52 * 1 \$\begingroup\$ @Sisyphus you're right, I misremembered! Commandline params to the compiler ARE permitted by standard loopholes! :) They just count towards the total, which is fine. \ $\endgroup\$ - Dewi Morgan Nov 29 '20 at 5:17 * 3 \$\begingroup\$ They don't actually count towards the total \$\ endgroup\$ - Jo King Nov 29 '20 at 5:45 * \$\begingroup\$ @JoKing You're joking, right? :D Well, I'm a noob, so may be misunderstanding. But, if that's the case, just about every compiler allows code to be passed in as parameters, so every code golf contest could be 0 characters for every language, and it would be quite boring indeed. So, most likely, I'm misunderstanding. Do you mean that, in php -r 'echo "hi";', the leading php -r ' and the trailing ' are not counted? \$\ endgroup\$ - Dewi Morgan Nov 29 '20 at 5:57 * 1 \$\begingroup\$ It's a bit confusing,, but here is the current meta consensus on flags. According to this, yes only the part inside the string parameter would be counted, though it would technically be the language PHP with the -r flag. Zero byte abuses of this would fall under the MetaGolfScript loophole \$\ endgroup\$ - Jo King Nov 29 '20 at 6:36 | show 2 more comments 11 \$\begingroup\$ HTML, [S:25:S] 23 bytes -2 bytes thanks to anonymoose Hello, World! --------------------------------------------------------------------- A side note on PHP: Inspired by @Dewi Morgan's PHP answer, I wanted to see if I could find any other approaches which did not use the backspace cheat. My first thought was that my HTML answer would instantly work as a PHP answer, however this is not the case. Even though PHP interprets everything outside of its tags as plain HTML, simply switching the extension from HTML to PHP causes sloc to interpret this as one line, and indeed it seems that sloc ignores HTML comments in PHP mode. I was hoping that something like //?>Hello, World! would solve this for PHP, but while sloc interprets this as 0 lines (hooray!!) PHP simply ignores the closing tag meaning that the entire construct is output to the page. So, for now I have to agree: it seems like a valid PHP solution without the backspace cheat (or using the -r flag) does not exist. share | improve this answer | follow | edited Dec 1 '20 at 7:08 answered Nov 29 '20 at 5:49 [4b8] ToastrackenigmaToastrackenigma 23111 silver badge66 bronze badges \$\endgroup\$ * 2 \$\begingroup\$ It seems that browsers also accept Hello, World! (23 characters), and sloc still counts it as a comment. From looking through the other solutions, this may be the shortest solution without "cheats." \$\endgroup\$ - anonymoose Nov 30 '20 at 14:00 * \$\begingroup\$ @anonymoose Thanks! \$\endgroup\$ - Toastrackenigma Dec 1 '20 at 7:09 * \$\begingroup\$ This is also valid PHP! \$\endgroup\$ - johannes 9 hours ago * \$\begingroup\$ @johannes It is, but sloc sees it as a line when set to PHP mode sadly :( \$\endgroup\$ - Toastrackenigma 9 hours ago add a comment | 9 \$\begingroup\$ Python, 32 bytes """'''""";print("Hello, World!") sloc outputs { total: 1, source: 0, comment: 1, single: 0, block: 1, mixed: 0, empty: 0, todo: 0, blockEmpty: 0 } Try it online share | improve this answer | follow | edited Nov 29 '20 at 14:55 [n42] caird coinheringaahing 22k55 gold badges5151 silver badges156156 bronze badges answered Nov 29 '20 at 13:10 [n8C] nobodynobody 24166 bronze badges \$\endgroup\$ add a comment | 7 \$\begingroup\$ Ruby, 46 bytes =begin =end =begin puts "Hello, World!" #=end sloc output ---------- Result ------------ Physical : 4 Source : 0 Comment : 4 Single-line comment : 0 Block comment : 4 Mixed : 0 Empty block comment : 0 Empty : 0 To Do : 0 Number of files read : 1 share | improve this answer | follow | answered Dec 10 '20 at 20:15 [4e0] MichaelMichael 17111 bronze badge \$\endgroup\$ * 1 \$\begingroup\$ Instead of puts, you could use p. The space can be omitted as well. To make it equivalent to puts, use p"Hello, World!\n" But I don't see the newline being required. \$\endgroup \$ - berkes 8 hours ago add a comment | 6 \$\begingroup\$ Perl5 $ cat script.pl #!/usr/bin/perl -Esay(Hello.chr(44).chr(32).world.chr(33)) $ ./script.pl Hello, world! sloc script.pl ---------- Result ------------ Physical : 1 Source : 0 Comment : 1 Single-line comment : 1 Block comment : 0 Mixed : 0 Empty block comment : 0 Empty : 0 To Do : 0 Number of files read : 1 ---------------------------- share | improve this answer | follow | edited Nov 29 '20 at 20:40 [ec3] Denis Ibaev 92455 silver badges66 bronze badges answered Nov 29 '20 at 13:52 [d0f] bessarabovbessarabov 16133 bronze badges \$\endgroup\$ * \$\begingroup\$ because it's code golf the number of bytes should appear currently 59, another solution 39 bytes or 20 bytes depending on counting \$\endgroup\$ - Nahuel Fouilleul Nov 30 '20 at 8:15 * 3 \$\begingroup\$ I'm not entirely sure this is actually Perl, I think this is your shell interpreting the file as when tested in TIO it complains that -E can't be set via shebang: Try it online! \$\endgroup\$ - Dom Hastings Nov 30 '20 at 8:17 * \$\begingroup\$ @DomHastings It must depend on your shell? > zero_line_hello_world.pl Hello, world! \$\endgroup\$ - Quantum Mechanic Dec 20 '20 at 11:30 * \$\begingroup\$ #!/usr/bin/perl -Esay'Hello World! (35 bytes) \$\ endgroup\$ - Quantum Mechanic Dec 20 '20 at 11:41 add a comment | 6 \$\begingroup\$ CSS, 38 bytes /**/:after{content:"Hello, World!"/**/ CSS, despite not being an actual progrtamming language, allows us to do basic output. This code creates a pseudo-element after every element on the page Sloc's block comment detection is broken (as seen in other answers) and this is reported as 0 lines of source code. See the sloc output here. The below snippet has been modified (40 bytes) so that the snippet runner and some browsers that style the (causing a double-output) display properly. /**/* :after{content:"Hello, World!"/**/ share | improve this answer | follow | edited Nov 30 '20 at 17:59 answered Nov 30 '20 at 4:13 [AOh] TheThunderGuySTheThunderGuyS 6955 bronze badges \$\endgroup\$ add a comment | 5 \$\begingroup\$ R, 43 bytes #!/usr/bin/rscript -e cat('Hello','World!') Count the zero source lines of code I'm not sure whether this is completely valid. It's a full program that must be launched directly from the shell (so it does not work in 'Try it Online' or 'rdrr.io' for instance, or when copy-pasted into the R console). Save as "helloworld.r" with execute permission, and run using ./helloworld.r or, more verbosely, exec helloworld.r > output.txt. R does not support multi-line comments, so the 'pairing the comment delimiter' tricks available in other languages aren't possible. The only (single line) comment character is #, and everything following it up to the next newline is not executed. However, the shebang line starting with #! is counted by "sloc" as a comment, even though it will be read and interpreted by the program loader. Usually, the shebang line simply tells the program loader which interpreter to use to run the rest of the program. So, this is a more-conventional 2-line (sloc=1) R program to accomplish the same task: #!/usr/bin/rscript cat('Hello','World!') But - we can also include command-line arguments in the shebang line, and the rscript interpreter accepts the -e flag to pass it the program code as an argument, which is what the sloc=0 code does here. So: the program is valid in R and the program code is run by R, but it requires to be launched from the shell and uses the help of the program loader. Is this valid? I don't know. But it's the only way that I could think of to 'trick' sloc in a language without multi-line comments... share | improve this answer | follow | edited Nov 29 '20 at 12:57 answered Nov 29 '20 at 12:36 [YIX] Dominic van EssenDominic van Essen 7,94411 gold badge55 silver badges1818 bronze badges \$\endgroup\$ * \$\begingroup\$ Why exec helloworld.r? That would replace your shell... \$\endgroup\$ - D. Ben Knoble Nov 29 '20 at 17:16 * \$\begingroup\$ @D.BenKnoble - I'll be honest: I don't really understand this stuff. I just tried it out both ways and it worked for me... \$\endgroup\$ - Dominic van Essen Nov 29 '20 at 17:22 * 8 \$\begingroup\$ I'm sorry, but I don't think this counts as R code. It uses a call to R, but you can't execute it by any of the standard ways of calling R code (i.e. with source, Rscript, R CMD BATCH...). The language here is something like "shell on a system with R installed", not R... \$\endgroup\$ - Robin Ryder Nov 29 '20 at 20:17 * \$\begingroup\$ In a similar vein, if "shell" were a language permitted by the challenge or if we consider this general approach valid, we could do #!/bin/sed s/.*/Hello, World!/ (31 bytes) \$\endgroup\$ - Nye Nov 30 '20 at 11:46 * 1 \$\begingroup\$ @RobinRyder - Yes - this is exactly why I wasn't sure about the validity of the answer (and I did point-out that it can't be run by the more-standard ways)... \$\endgroup\$ - Dominic van Essen Nov 30 '20 at 12:28 | show 3 more comments 5 \$\begingroup\$ PHP, with various commandline shenanigans, 0-33 Depending how I read Command-line flags on front ends, various of these answers may apply. I feel none do, so downvotes are fine and expected on this one :) I'm posting it more as an exploration of the problem-space, perhaps towards an eventual argument that a solution is not possible in PHP, than an answer that I expect to be accepted. I've given my arguments against them all, summarized with opinionated bold statements, because that naturally makes the arguments more true. I'm very interested to hear counterarguments, or additional arguments, in the comments! php -r 'echo"Hello, World!";', 0 One interpretation is that nothing on the commandline counts, but that the entire commandline is treated as a kind of "language dialect name" for the entry. Against: All code should be counted. Otherwise, this approach makes any contest where bytes matter trivial in almost any language, by offloading all work to the CLI. The parameters to the -r flag here are not a "flag to choose a language option", they are "code", and should be counted as such. PHP -r, 20 php -r 'echo"Hello, World!";' It could be argued that, while we count the code on the CLI, the sloc requirement only applies to the contents of source files. If there is no source file, then there are no code lines in source files! Against: All counted code should be passed to sloc. Every language lets you slap code in the cli params. The challenge isn't meant to degrade into "run hello world without using a source file", so this isn't an interesting solution to the challenge. PHP -r ignoring quotes, 29 php -r '/**/echo "Hello, World!";/**/' So we can pass in a minimal code sample on the commandline, getting all the benefits of avoiding PHP's