112
\$\begingroup\$

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 ab and 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 , 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>

\$\endgroup\$
22
  • 2
    \$\begingroup\$ Related. Related. \$\endgroup\$ May 17, 2017 at 11:19
  • 22
    \$\begingroup\$ "Your code must not be a quine" but... but... it's tagged quine \$\endgroup\$
    – Okx
    May 17, 2017 at 11:21
  • 4
    \$\begingroup\$ @Okx Because it's a generalised quine, i.e. the required output depends on the source code. \$\endgroup\$ May 17, 2017 at 11:22
  • 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
    May 17, 2017 at 22:37
  • 2
    \$\begingroup\$ @WheatWizard output by exit code is not a string, so it doesn't apply here. \$\endgroup\$ May 18, 2017 at 4:29

288 Answers 288

1
3 4
5
6 7
10
2
\$\begingroup\$

Perl, 8 7 bytes

print$␖

The seventh byte of this program is the control character \x16, which I can't put literally into the writeup. A hexdump of the source is 70 72 69 6e 74 24 16.

Output is something like v5.20.2 on the standard output. The exact output depends on the exact version, but almost always 7 characters in practical situations. There is no newline or whitespace in the source code or output.

Note that running this code also prints a long mandatory warning message to the standard error. If I understand correctly, that message is ignored by the golf rules.


One byte longer (but all printable) is this program:

;print**

The semicolon is there to pad the code to the same length as the output. The output is *main::*

\$\endgroup\$
2
\$\begingroup\$

Stax, 1 byte

æ

Run and debug online!

Bytes counted in CP437.

Just the packed version of the program 1. Not surprisingly the output is also 1. Since it is packed it is not a quine and is a valid answer.

\$\endgroup\$
2
\$\begingroup\$

VBA, 1 Byte

A surprisingly short answer for VBA

Anonymous VBE immediate window function that takes no input and outputs a single linefeed (vbLf) to the immediate window

?

Output


(A single linefeed)

\$\endgroup\$
2
\$\begingroup\$

Pascal (FPC), 22 bytes

begin write('':22)end.

Try it online!

:22 pads the write value to be at least 22 characters in length. Here, the write value is an empty string, so this prints 22 spaces.

\$\endgroup\$
1
  • \$\begingroup\$ −1 Byte: You do not need a string literal. begin write(1:21)end. \$\endgroup\$ Mar 9 at 13:21
2
\$\begingroup\$

Javascript (ES6), 5 bytes

!1+''

outputs the string "false".

\$\endgroup\$
2
\$\begingroup\$

R, 25 bytes

sprintf("%.23f",runif(1))

Try it online!

Outputs (as a string) a random number between 0 and 1 with 23 decimal places, e.g.

0.84327139146625995635986

This is 25 characters overall, counting the initial 0.

There are shorter R solutions, but I wanted to post one with randomness since the challenge allows it explicitly.

\$\endgroup\$
2
  • \$\begingroup\$ At least on TIO this produces output of different length. Caught both 17 (0.703815360320732) and 19 (0.01503348327241838). \$\endgroup\$
    – manatwork
    May 3, 2019 at 13:50
  • \$\begingroup\$ @manatwork Thanks! This new version should work consistently. \$\endgroup\$ May 3, 2019 at 18:27
2
\$\begingroup\$

brainfuck, 15 32 bytes

++++++++[>++++>++++<<-]>+>[<.>-]

Try it online!

Illegal version (no ascii), 15 bytes

->+++++[<...>-]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Oh, I didn't have in mind that ascii is only up to character 0x7e and not 0xff. I made a new version. \$\endgroup\$
    – Dorian
    Jul 30, 2019 at 10:47
2
\$\begingroup\$

brainfuck, 24 bytes

-[>+<---]++++++[->....<]

Try it online!

Prints 24 Us.

Explanation:

-[>+<---]    # Generate a U
++++++[      # Loop 6 time
->....<]     # Printing U 4 times each
\$\endgroup\$
2
\$\begingroup\$

Pyramid Scheme, 32 bytes

^ ^ ^
-^-^-
^-^-
-^-^
^-^-
- -


Try it online!

Outputs 16 zeroes and newlines. I can find a lot of alternative 32 byters with varying amounts of extra padding, but the closest I can come to something shorter is this 27 byte program that outputs 26 bytes

\$\endgroup\$
2
\$\begingroup\$

Zsh, 5 bytes

<<<$-

Prints 569X followed by a newline. Try it online!

The parameter $- outputs the current flags supplied to the shell. By default, it is set to 569X:

  • 5: NOTIFY
  • 6: BG_NICE
  • 9: AUTO_LIST
  • X: LIST_TYPES

When unsetting these options, $- is empty.

\$\endgroup\$
1
  • \$\begingroup\$ you is a tricksy hobbitses. nice answer \$\endgroup\$
    – roblogic
    Jul 13, 2023 at 6:34
2
\$\begingroup\$

Actually, 2 bytes

Not winning of course, but I find it relatively interesting. (outputs 10)

1╤

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Keg, 1 byte (SBCS)

🄂

Try it online!

Now before you say "Hang on a second, that's 2 bytes: a 1 and a ,", it isn't. Unicode has these really nice symbols where they have a number and a comma combined. It push'n'prints the number 1, the length of the program.

Just don't go pasting this into programs like IDLE and tkinter... they don't particularly like 🄂.

\$\endgroup\$
4
  • \$\begingroup\$ I don't see that character in the codepage for the language. What byte value does it have? \$\endgroup\$
    – Jo King
    Oct 14, 2019 at 0:49
  • \$\begingroup\$ @JoKing 248. You don't see it because otherwise I couldn't use IDLE to edit Keg.py (as mentioned in my answer 🄂 isn't very much supported) \$\endgroup\$
    – lyxal
    Oct 14, 2019 at 1:38
  • \$\begingroup\$ Perhaps move the code page to its own file, since you shouldn't have to edit it much? \$\endgroup\$
    – Jo King
    Oct 14, 2019 at 1:47
  • \$\begingroup\$ @JoKing I've been trying to implement the code page using Mego's golflang-encoding repository to no avail. \$\endgroup\$
    – lyxal
    Oct 14, 2019 at 1:54
2
\$\begingroup\$

Go, 22 bytes

func f(){print(0., f)}

On my machine, this prints

+0.000000e+0000x477c88

But the compiler could conceivably allocate f at an address other than 6 hex digits. If you don't like an implementation detail like that, the next best thing is the 26-byte

func f(){print(0.,1<<34)}

which prints

+0.000000e+00017179869184
\$\endgroup\$
2
\$\begingroup\$

Python 3, 29 bytes

s='s=%r;print(ss)';print(s%s)

Try it online!

Output

s='s=%r;print(ss)';print(ss)

A more quine-like answer than this answer

\$\endgroup\$
0
2
\$\begingroup\$

MAWP, 13 bytes

43W2M[84W;1A]

Prints 14 space characters.

Try it!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice, but here codegolf.stackexchange.com/a/209253/92080 is a 1 byter :) \$\endgroup\$
    – Dion
    Aug 8, 2020 at 10:45
  • \$\begingroup\$ I thought the . was required for output. Anyway, the question asks for an ASCII character so I made this. \$\endgroup\$
    – Razetime
    Aug 8, 2020 at 10:50
  • 3
    \$\begingroup\$ Well, to be fair the output not appearing without a . was a bug that is now fixed, my bad. And 1 is also an ASCII character (49) \$\endgroup\$
    – Dion
    Aug 8, 2020 at 14:25
2
\$\begingroup\$

05AB1E, 1 byte

,

Try it online!

,  # output top of stack (nothing) with trailing newline
\$\endgroup\$
2
\$\begingroup\$

Rust, 30 28 bytes

fn main(){print!("{:28}",0)}

Try it online!

Prints zero, padded with enough spaces so that it is 28 bytes long.

\$\endgroup\$
2
\$\begingroup\$

MAWP, 1 bytes

:

Try it!

Prints the 1 thats already on stack

\$\endgroup\$
4
  • \$\begingroup\$ A fun but longer solution is M:. \$\endgroup\$
    – Jo King
    Aug 8, 2020 at 12:54
  • \$\begingroup\$ @JoKing I'm a bit confused. The link you are giving points a completely different submission, while M:. would result in an error: M needs two numbers off the stack whereas the starting stack is only [1]. Could you please clarify what you meant? \$\endgroup\$
    – Dion
    Aug 8, 2020 at 14:23
  • \$\begingroup\$ Oh, weird, the generate link didn't work the way I expected it to. I meant this, which prints NaN \$\endgroup\$
    – Jo King
    Aug 8, 2020 at 14:40
  • \$\begingroup\$ @JoKing that is actually a really smart solution imo, feel free to post it as another solution \$\endgroup\$
    – Dion
    Aug 8, 2020 at 18:36
2
+50
\$\begingroup\$

Red, 32 bytes

i: 1 until[prin i: i + 1 i > 20]

Try it online!

Sets i to 1, then increments i and prints it without trailing newlines until i is greater than 20. This prints the numbers [2..21] without delimiters, an output 32 characters long.

\$\endgroup\$
2
\$\begingroup\$

tinylisp, 3 bytes

(q(

Try it online!

Explanation

After parenthesis autocompletion, the code is (q()). The q macro prevents evaluation of its argument, which in this case is the empty list (). So the program outputs (), with a trailing newline.

\$\endgroup\$
1
2
\$\begingroup\$

Ruby 3, 6 bytes

p ?c*3

Try it online!

Prints "ccc" with a trailing newline to get a 6-byte response.

\$\endgroup\$
2
\$\begingroup\$

Halfwit, 1 byte

><

Try It Online!

The compressed integer 0, which currently prints 0n.*

*I'm going to change this soon

Other interesting approaches:

Halfwit -A, 2 bytes

4+

Try It Online!

The -A flag turns characters into bytes. 4 is a predetermined constant [0, 1] and + is a NOP here.

Halfwit -A, 3 bytes

>;<M>f

Try It Online!

This is Mapping 3 (>;<) (converted to a range 1, 2, 3 on map) to 8 (>f), which then gets turned into characters and output.

\$\endgroup\$
2
\$\begingroup\$

Aheui (esotope), 30 bytes

받발땨희빠챠뎌뱧볗멍

Try it online!

Prints 15 copies of -1.

Aheui (esotope), 30 bytes

밤발땨희빠챠명뱧뼈더

Try it online!

Prints 191817161514131211109876543210, i.e. a countdown from 19 to 0 without spaces.

How these work

Aheui is a 2D language with Hangul characters. Each Hangul character consists of an initial consonant, a vowel, and an optional final consonant. In Aheui, the initial consonant defines the instruction, the vowel defines the next movement, and the final consonant provides an argument to the instruction (when necessary).

Aheui comes with multiple stacks and a queue, but the programs above are simple enough that they only operate on a single stack.

Relevant consonants:

  • Initial consonant ㅂ: Pushes a constant or a value from stdin.
    • Final consonant ㄷ, ㄹ, ㅁ: Pushes 3, 5, 4 respectively.
    • ㅎ: Reads a UTF-8 character from stdin and pushes its value. On EOF, pushes -1.
  • Initial consonant ㄷ, ㄸ: Pops two values and pushes the sum and product respectively.
  • ㅃ: Duplicates the top value.
  • ㅁ: Pops a value, and prints as integer if the final consonant is ㅇ.
  • ㅊ: Pops a value. If it is 0, the next movement is reversed.
  • ㅎ: Halts the program.

Relevant vowels:

  • ㅏ, ㅓ, ㅗ, ㅜ: Move in the direction of the short stroke by 1 cell.
  • ㅑ, ㅕ, ㅛ, ㅠ: Move in the same direction, but by 2 cells (jumping over a cell).

The two-cell movements allow simple loops to be encoded in a single line, and each character encoding both the instruction and the movement allows more flexible arrangement of code fragments.

받발땨희빠챠뎌뱧볗멍
받발땨
push 3; push 5; mul (i.e. push 15); enter loop with this loop counter
  빠챠
  if the current loop counter is 0, direct to 희 and halt
  뱧 멍 볗 뎌
  push -1, print, push -1, add

밤발땨희빠챠명뱧뼈더
밤발땨
push 4; push 5; mul (i.e. push 20); enter loop
  빠챠 (same as the other program)
  뱧 더 뼈 명
  push -1, add, dup, print

More info can be found in the language spec.

\$\endgroup\$
2
\$\begingroup\$

JavaScript REPL, 2 bytes

~0

So, apparently there is a limit to how small your submissions can be. Typing words here so I can submit. \$\$

\$\endgroup\$
2
  • 5
    \$\begingroup\$ It only works in the REPL environment, not as regular code in a .js file - you need to call console.log or similar to actually output anything. So you should label your language as "JavaScript REPL". \$\endgroup\$
    – Bubbler
    Oct 18, 2021 at 4:47
  • \$\begingroup\$ Polyglots with Python REPL \$\endgroup\$ Jul 6, 2023 at 15:34
2
\$\begingroup\$

ForWhile 6 bytes

6(48#)

prints 6 zeros

5(9#)

would print 5 tabs (but tab is not an allowed character)

online interpreter

Explanation

6(   \ start for-loop with 6 iterations
48#  \ print 0
)    \ end for-loop
\$\endgroup\$
2
\$\begingroup\$

Julia 1.0, 6 bytes

@show^

Try it online!

Prints ^ = ^ followed by a newline.

\$\endgroup\$
2
\$\begingroup\$

Electra, 18 15 bytes

NONO<??
ONO---o

Prints 15 zeros

\$\endgroup\$
1
\$\begingroup\$

PHP, 17 bytes

<?php echo 9**17;

Because I wanted to beat Mayube's answer ;)

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Touche. I'll get you next time ;) \$\endgroup\$
    – Mayube
    May 17, 2017 at 11:38
  • \$\begingroup\$ Looks like I got you, kind of :D \$\endgroup\$
    – Mayube
    May 17, 2017 at 12:15
  • 2
    \$\begingroup\$ same way golfed <?=9**8; \$\endgroup\$ May 17, 2017 at 13:09
  • 1
    \$\begingroup\$ Sorry can't beat Mayube/Jörg's byte count but at least I can give a good and creative tip: <?=1/64;. \$\endgroup\$
    – Christoph
    May 17, 2017 at 13:10
1
\$\begingroup\$

Go, 60 bytes

I don't know if this is hard in Go or if I'm failing to see something obvious.

import(."fmt"
."strings")
func main(){Print(Repeat("a",60))}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Can you do something like 1e30 or 10^49? \$\endgroup\$ May 17, 2017 at 19:03
  • \$\begingroup\$ @CalculatorFeline 1e30 would print 1e+37 and 10^49 would print 37 (due to bitwise operation). \$\endgroup\$ May 22, 2017 at 20:24
1
\$\begingroup\$

brainfuck, 33 bytes

>+[-->---[-<]>]>+[->+>+<<]>[->.<]

Try it online!

Output:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I could do it in 32 bytes but I decided this is better.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Then do it in 32 bytes. \$\endgroup\$ May 17, 2017 at 19:04
  • \$\begingroup\$ You should do it in 32 bytes or else it isn't a serious competing entry. \$\endgroup\$ May 24, 2017 at 13:42
1
3 4
5
6 7
10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.