In this challenge, you should write a program or function which takes no input and prints or returns a string with the same number of bytes as the program itself. There are a few rules:
- You may only output bytes in the printable ASCII range (0x20 to 0x7E, inclusive), or newlines (0x0A or 0x0D).
- Your code must not be a quine, so the code and the output must differ in at least one byte.
- Your code must be at least one byte long.
- If your output contains trailing newlines, those are part of the byte count.
- If your code requires non-standard command-line flags, count them as usual (i.e. by adding the difference to a standard invocation of your language's implementation to the byte count), and the output's length must match your solution's score. E.g. if your program is
aband requires the non-standard flag-n(we'll assume it can't be combined with standard flags, so it's 3 bytes), you should output 5 bytes in total. - The output doesn't always have to be the same, as long as you can show that every possible output satisfies the above requirements.
- Usual quine rules don't apply. You may read the source code or its size, but I doubt this will be shorter than hardcoding it in most languages.
You may write a program or a function and use any of the standard methods of providing output. Note that if you print the result, you may choose to print it either to the standard output or the standard error stream, but only one of them counts.
You may use any programming language, but note that these loopholes are forbidden by default.
This is code-golf, so the shortest valid answer – measured in bytes – wins.
Leaderboard
var QUESTION_ID=121056,OVERRIDE_USER=8478;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){var F=function(a){return a.lang.replace(/<\/?a.*?>/g,"").toLowerCase()},el=F(e),sl=F(s);return el>sl?1:el<sl?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;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="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><div id="language-list"> <h2>Winners 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><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>
-
2\$\begingroup\$ Related. Related. \$\endgroup\$Martin Ender– Martin Ender2017年05月17日 11:19:42 +00:00Commented May 17, 2017 at 11:19
-
1\$\begingroup\$ @Mr.Xcoder Yes, if you declare them as such. \$\endgroup\$Martin Ender– Martin Ender2017年05月17日 16:25:14 +00:00Commented May 17, 2017 at 16:25
-
1\$\begingroup\$ @DJMcMayhem I figured that in many esolangs it's trivial to print a null byte with one byte of code or similar things, and thought it would lead to more interesting answers if the results were actually readable. \$\endgroup\$Martin Ender– Martin Ender2017年05月17日 16:45:23 +00:00Commented May 17, 2017 at 16:45
-
4\$\begingroup\$ @MartinEnder You should probably disallow output by exit code, which is a default. If you allow it nearly every one byte program in nearly every language is allowed. One user has already done this \$\endgroup\$Wheat Wizard– Wheat Wizard ♦2017年05月17日 22:37:40 +00:00Commented May 17, 2017 at 22:37
-
2\$\begingroup\$ @WheatWizard output by exit code is not a string, so it doesn't apply here. \$\endgroup\$Martin Ender– Martin Ender2017年05月18日 04:29:06 +00:00Commented May 18, 2017 at 4:29
291 Answers 291
JavaScript (ES6), 17 bytes
Edit: I overlooked the rules. This is now returning a string, but is much longer than initially intended.
Returns "Infinity,Infinity".
let f =
_=>`${[1/0,1/0]}`
console.log(f())
C#, (削除) 57 (削除ここまで) (削除) 49 (削除ここまで) (削除) 47 (削除ここまで) 44 bytes
()=>{for(int i=0;i<44;i++)Console.Write(7);}
-8 bytes thanks to Martin
Not 100% on whether lambdas like this are accepted answers, but my previous submission in this format was accepted just fine, so I'm gunna go with it.
Same as the java answer, but better because it's not Java
Deadfish, 1 byte
o
Outputs the accumulator, which is 0 before any action takes place.
note: deadfish prints the accumulator as a number, not as a character code,
so the output is "0" (0x48)
QBIC, (削除) 8 (削除ここまで) 2 bytes
?z
Prints 10, to which z is auto-initialised.
Original (削除) brainfart (削除ここまで) answer
?A+@1234
Explanation:
? PRINT
A+ A$ (which is undefined, but hold on!), plus
@1234 The string literal 1234, which we now call A$
Outputs 12341234, which is also 8 bytes. We save a byte by putting the string lit at EOF, because we now don't need to use the delimiter. The definition of the literal is moved to the top of the QBasic code by the interpreter, ensuring it existst at the moment of the first call to A$.
-
\$\begingroup\$ This seems to be 11 bytes of output, not 12. \$\endgroup\$DJMcMayhem– DJMcMayhem2017年05月17日 16:34:31 +00:00Commented May 17, 2017 at 16:34
-
\$\begingroup\$ @DJMcMayhem You're right, fixed \$\endgroup\$2017年05月17日 16:39:51 +00:00Commented May 17, 2017 at 16:39
-
\$\begingroup\$ It's to bad
-vprintBrain-Flak Ruby Interpreter v1.4.2instead of justv1.4.2. \$\endgroup\$Riley– Riley2017年05月17日 23:30:23 +00:00Commented May 17, 2017 at 23:30
Stacked, 9 bytes
$put:+put
Outputs [put put]. $put pushes a function literal to the stack, : duplicates it, + concats the two functions, and put outputs the representation of the top of the stack.
Haskell, 16 bytes
main=print[0..6]
Try it online! Output:
[0,1,2,3,4,5,6]
(Note the trailing newline.)
MSM, 8 bytes
'.;;.;.;
Output:
........
MSM operates on its own source and takes commands from the left and treats the right as a stack. Stack trace:
' . ; ; . ; . ; # ' pushes the next char on the stack
; ; . ; . ; . # ; is dup
; . ; . ; . . # dup again
. ; . ; . . . # . is concat
; . ; . .. # dup
. ; . .. .. # concat
; . .... # dup
. .... .... # concat
........ # MSM stops if there's only one element in the stack
brainbool, 1 byte
.
brainbool is like brainfuck, except cells can only contain 0 or 1. Outputting in brainbool always produces an ASCII 0 or 1. All cells start out as 0, so . will output 0.
Python 2, 11 bytes
print 4**16
Beep boop.
-
1\$\begingroup\$ You can do
8**9for 10 bytes. \$\endgroup\$PurkkaKoodari– PurkkaKoodari2017年05月17日 12:14:44 +00:00Commented May 17, 2017 at 12:14 -
1\$\begingroup\$ @Pietu1998 I was about to do
9**9, but @DeadPossum ninja'd me, so I'd rather keep this... \$\endgroup\$Yytsi– Yytsi2017年05月17日 12:16:39 +00:00Commented May 17, 2017 at 12:16
Python 3 REPL, 3 bytes
1e0
Prints 1.0.
If trailing newline counts,
5*2
Prints 10, then a trailing newline.
-
1\$\begingroup\$ Yes, the trailing linefeed counts. \$\endgroup\$Martin Ender– Martin Ender2017年05月18日 04:36:52 +00:00Commented May 18, 2017 at 4:36
TacO, 1 byte
@
Outputs a single newline.
Taco's default behaviour is to print out a newline, for some reason. But, the code needs an entry point, @, to work. So this works.
Bash, 11 bytes
printf %11s
prints:
" "
(without the quotes)
If the newline counts, then printf "%10s" will print 10 characters plus the newline.
I did consider echo 0ドル (7) which works when saved with a filename 7 characters long, but I think that falls under the common set of unaccepted answers
-
2\$\begingroup\$
printf %11soutputs exactly 11 spaces \$\endgroup\$Digital Trauma– Digital Trauma2017年05月17日 15:54:33 +00:00Commented May 17, 2017 at 15:54
Brian & Chuck, 9 bytes
?{-?
.{?
Prints ?????????.
The first character on the second line is a tab.
Explanation
The tab's code point is 9 and it's just used as a counter to loop the correct number of times.
The program starts by immediately switching to Chuck with ?. The . prints that ?, { currently does nothing and ? switches back to Brian.
We now start the main loop which runs 8 times. { moves the tape head on Chuck back to the first cell, i.e. the tab. - decrements this counter. Once the counter hits zero, ? does nothing and the program ends. But until that happens it switches control back to Chuck. Then . prints another ?, { resets the tape head on Brian to the beginning and ? hands control back to him. This way, the two instances alternate between decrementing the counter on Chuck and printing a ? on Brian.
Japt, 1 byte
Outputs a string containing a single ".
Q
Or this will output a string containing a single \n.
R
-
\$\begingroup\$ Heh, nice.
Swill work too, orT, orU, ora, orf, or... there are probably 40 or so options that print0or1. \$\endgroup\$ETHproductions– ETHproductions2017年05月19日 00:46:23 +00:00Commented May 19, 2017 at 0:46 -
\$\begingroup\$ @ETHproductions,
T,U,a,f, etc. wouldn't be valid as string output is required.sworks, though. \$\endgroup\$Shaggy– Shaggy2017年05月19日 07:44:05 +00:00Commented May 19, 2017 at 7:44 -
\$\begingroup\$ Only functions are required to return strings. The output of any program is considered a string by default, since there's no way to tell between outputting
"123"vs.123(both just show up as123) \$\endgroup\$ETHproductions– ETHproductions2017年05月19日 12:02:49 +00:00Commented May 19, 2017 at 12:02 -
\$\begingroup\$ Unless, in the case of Japt, you use the
-Qflag ;) But that's good to know, thanks @ETHproductions. \$\endgroup\$Shaggy– Shaggy2017年05月19日 12:04:53 +00:00Commented May 19, 2017 at 12:04
7, 3 characters, 1 byte
Seen as individual characters:
723
Or as a hex dump, proving that it runs from a file that's one byte long:
00000000: e9 .
Outputs the characters 23 in 7's encoding. This also happens to be a single byte long, and a printable ASCII byte at that:
00000000: 4f O
Explanation
This is a literal that pushes two stack elements: an empty stack element (7 separates stack elements), and 23, 7's print statement. When the end of the program is reached, the top stack element is copied and evalled, meaning that an attempt is made to print 23; because this contains characters that don't exist in 7's encoding (the active versions of the 2 and 3 commands, shown in bold in the explanation), the printed value gets escaped (into 723), and the 7 is interpreted as a request to format the output in the same encoding as the input, with the 23 getting printed.
It can be observed that the leading 7 on the program is entirely pointless, except to make the output and input distinct.
Ruby, 16 bytes
puts (1..8).to_a
returns
1
2
3
4
5
6
7
8
Can I use this to enter challenge?
Try it Online!
-
1\$\begingroup\$ Looks good to me. Welcome to PPCG! :) \$\endgroup\$Martin Ender– Martin Ender2017年05月23日 08:57:24 +00:00Commented May 23, 2017 at 8:57
-
\$\begingroup\$
p (1..4).to_ashould work, for 13 bytes. \$\endgroup\$Dennis– Dennis2017年05月23日 17:50:12 +00:00Commented May 23, 2017 at 17:50 -
\$\begingroup\$ @Dennis and codegolf.stackexchange.com/a/121108/6691 is even shorter and already posted, so what's the point of that comment? \$\endgroup\$Matt Cloudy-grid– Matt Cloudy-grid2017年06月13日 20:09:48 +00:00Commented Jun 13, 2017 at 20:09
-
\$\begingroup\$ @b_jonas Just because someone achieved a better score doesn't mean you can't try to get the most of your approach. \$\endgroup\$Dennis– Dennis2017年06月13日 20:15:04 +00:00Commented Jun 13, 2017 at 20:15
ZX Spectrum BASIC, 1 byte
PRINT
(which is 1 byte) prints a single newline.
-
\$\begingroup\$ I think Apple][ integer Basic does a similar thing with
?forPRINT\$\endgroup\$roblogic– roblogic2019年08月15日 02:02:59 +00:00Commented Aug 15, 2019 at 2:02
dc (bash command line under Linux), 9 bytes
(inc newlines on both the math expression and the answer)
2 24 ^ p
Prints:
16777216
I wanted to make sure RPN wasn't forgotten about :)
-
\$\begingroup\$ Welcome to PPCG! You're a little bit outgolfed already. \$\endgroup\$Ørjan Johansen– Ørjan Johansen2017年05月23日 17:53:18 +00:00Commented May 23, 2017 at 17:53
-
\$\begingroup\$ @ØrjanJohansen, hehe yes I don't mind, to be honest I just wanted to play along. The day that P8x32A Assembly language is a valid language around here I'll whup everyone :D \$\endgroup\$Wossname– Wossname2017年05月23日 17:55:36 +00:00Commented May 23, 2017 at 17:55
-
\$\begingroup\$ You're free to use any language you want (as long as there's a working implementation), including P8x32A assembly. \$\endgroup\$Dennis– Dennis2017年05月23日 17:58:28 +00:00Commented May 23, 2017 at 17:58
-
\$\begingroup\$ @Dennis, define "working". I doubt many PCG frequenters would feel like grabbing a soldering iron and building a circuit to prove that the code works :) \$\endgroup\$Wossname– Wossname2017年05月23日 18:00:08 +00:00Commented May 23, 2017 at 18:00
-
1\$\begingroup\$ So basically it needs an emulator. \$\endgroup\$Ørjan Johansen– Ørjan Johansen2017年05月23日 18:03:14 +00:00Commented May 23, 2017 at 18:03
Microscript, 1 byte
E
Prints 100=1. e will also work, printing 20=1.
Forth (gforth), 4 bytes
.S \
Prints <0> (that's <, 0, >, and space). Stack must be empty (for example if gforth was just started)
How it works: .S prints the stack depth enclosed in brackets and then the stack contents seperated with a space. \ is the beginning of a comment, it does nothing.
Forth (gforth), 8 bytes
8 SPACES
Well, it prints 8 spaces. Not much to see here.
SmileBASIC 3, 3 bytes
?@A
Prints the string @A, plus a trailing newline, so output is 3 bytes.
TI-Basic, 7 bytes
(Ab)using the fact that Done is displayed when nothing is evaluated on the last line. Lowercase letters are two bytes each in TI-Basic.
Repeat 1337:End
-
1\$\begingroup\$ I wouldn't count printed lowercase letters as two bytes. Lowercase letter tokens take two bytes to store, but I think the calculator doesn't store the tokens when it prints the letters. \$\endgroup\$lirtosiast– lirtosiast2017年07月11日 01:36:27 +00:00Commented Jul 11, 2017 at 1:36
-
1\$\begingroup\$ @lirtosiast You could always have something like
Repeat 1:Endfor 4 bytes of course. \$\endgroup\$Timtech– Timtech2017年07月11日 02:21:21 +00:00Commented Jul 11, 2017 at 2:21 -
1\$\begingroup\$ Yes, that would be better \$\endgroup\$lirtosiast– lirtosiast2017年07月11日 16:19:41 +00:00Commented Jul 11, 2017 at 16:19
JavaScript, 35 bytes
Source:
console.log(([]+[])["constructor"])
Output:
function String() { [native code] }
HQ9+, 13 bytes
haaaaaaaaaaaa
Prints:
Hello, world!
The instruction "h" in HQ9+ will print "Hello, world!" and any instruction that is not "h" "q" "9" or "+" will be ignored.
You might want to know why this language exists, and why I'm answering this question with it.
Why not?
Python 3 REPL, 11 bytes
str(...)[:]
Try it online! The header and the footer emulate a REPL.
This prints:
'Ellipsis'