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

287 Answers 287

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

><>, 3 bytes

"nh

Try It Online

Prints "104" and exits with an error.

\$\endgroup\$
4
\$\begingroup\$

Dash/Bash/ksh/fish, 5 bytes

umask

Try it online (Dash) | Try it online (Bash) | Try it online (Ksh) | Try it online (Fish)

umask is a builtin in most unix shells, not an external command! It prints the 4-octal-digit umask followed by a newline for a total of 5 bytes. Does not work in Zsh or Tcsh: Zsh will only print one leading zero (e.g.: 02 instead of 0002), and Tcsh will print no leading zeroes (2 instead of 0002)

\$\endgroup\$
4
\$\begingroup\$

C# (Visual C# Interactive Compiler), 14 bytes

_=>new[]{1}+""

Found interesting way to shave some bytes. Outputs: System.Int32[]

Try it online!

First way: 23 bytes

()=>new string('@',23);

Creates a new string consisting of 23 @ characters

\$\endgroup\$
4
\$\begingroup\$

AWK, 19 16 bytes

END{print 69**8}

Try it online!

Thanks to Jo King for the pointer that cut 3 chars...

AWK is a bit of a pain for this one since the minimum code required to run without any input and print something is END{print }. So this one just prints a number that's enough digits to match the program size less one, since the print adds a linefeed. The output is 513798374428641\n.

\$\endgroup\$
0
4
\$\begingroup\$

Python 3, 13 12 bytes

print(2**-9)

Try it here!

Output:

0.001953125


Old:

print([1]*4);

Try it here!

Output:

[1, 1, 1, 1]

-1 Thanks to aeh5040! With this, the unneeded semi-colon could be removed as the output is 11 bytes + white space

A list printed in python would have the length of 2 + 3 for each extra element plus the new-line, thus with 1*n we can have any multiple of 2+3n. A semi-colon is added because unfortunately print adds a new-line character, and I've yet to find a better solution

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

Desmos, 2 bytes

4!

Outputs

24
\$\endgroup\$
3
\$\begingroup\$

Powershell, 3 bytes

1E2

Prints 100

\$\endgroup\$
2
  • \$\begingroup\$ Looks like you (hopefully accidentally) added a leading space to your code. \$\endgroup\$
    – Mayube
    May 17, 2017 at 12:06
  • 1
    \$\begingroup\$ PowerShell adds a trailing newline. Newlines count for this one. 1e1 would work though \$\endgroup\$
    – Matt
    May 25, 2017 at 17:47
3
\$\begingroup\$

PHP, 216 68 7 bytes

<?=1e6;

Try it online!

Thanks to Jörg I'm beating Okx again :D

\$\endgroup\$
1
  • 3
    \$\begingroup\$ <?=1e6; 7 Bytes \$\endgroup\$ May 17, 2017 at 12:10
3
\$\begingroup\$

TAESGL, 1 byte

S

Interpreter

Outputs a single space character

Other solutions

"≠        2 bytes, "≠" converted to "!="
«Ā»       3 bytes, decompresses "Ā" which is equal to "the"
SŔ4)      4 bytes, " " repeated 4 times
5ē)ĴT     5 bytes, first 5 Fibonacci numbers joined
G→6,"A    6 bytes, draws a line to the right of "A" for 6 characters
\$\endgroup\$
1
  • \$\begingroup\$ This is also a CJam program. \$\endgroup\$
    – Xwtek
    Mar 4, 2021 at 17:56
3
\$\begingroup\$

Stack Cats, 1 + 3 = 4 bytes

+

Try it online!

Requires either the -o or -n flag for numeric output. Prints two zeros, with a linefeed each.

Explanation

Stack Cats has a tape of stacks which are all initialised to an infinite wells of zeros, but the starting stack has a -1 on top (which acts as a terminator when input is given). The + command swaps the first and third element on the current stack. So:

-1           0
 0           0
 0     +    -1
 0   ---->   0
 .           .
 .           .
 .           .

At the end of the program, the current stack is printed from top to bottom. Since the -1 is again treated as a terminator, it's not printed itself. Due to the -o flag, the values are printed as decimal integers with trailing linefeeds.

\$\endgroup\$
3
\$\begingroup\$

Octave, 7 bytes

[['']]'

Try it online!

\$\endgroup\$
3
\$\begingroup\$

TI-Basic (TI-84 Plus CE OS 5.2+), 4 bytes

toString(10^(3

tostring( is a two-byte token, 10^( is a one-byte token. This returns the string "1000" which is 4 bytes long.

\$\endgroup\$
2
  • \$\begingroup\$ @Scrooble toString( is a two byte token, so the output does need to be 4 bytes, not 3, even though it has 3 tokens \$\endgroup\$ Sep 10, 2017 at 4:43
  • \$\begingroup\$ I know. I probably shouldn't post anything at 12:30 AM to avoid embarrassment. I was too slow to recall my comment before you saw it. \$\endgroup\$ Sep 10, 2017 at 4:46
3
\$\begingroup\$

Fission, 4 bytes

R"N;

Try it online!

Prints N;R with a trailing linefeed.

The R creates a right-going atom. " toggles string mode which traverses an immediately prints N;R (wrapping at the end of the line). Then N prints a linefeed and ; destroys the atom, terminating the program.

\$\endgroup\$
3
\$\begingroup\$

Somme, 2 bytes

:.

Try it online!

Outputs 42. Explanation:

:.
:    duplicate; no input, so popping from an empty stack pushes `42`
 .   output as a number
\$\endgroup\$
1
  • 1
    \$\begingroup\$ popping from an empty stack pushes 42 ಠ_ಠ \$\endgroup\$
    – MD XF
    Sep 12, 2017 at 3:43
3
\$\begingroup\$

Bash + coreutils (on 32-bit Linux), 5 bytes

arch
(trailing newline)

Prints

i686
(trailing newline)

Old solution:

sed s/e/a $0

Save (without trailing newline) in filename, and run with bash filename.

Prints

sad s/e/a $0

with no newline. Really you could replace any character in the file with a different character, but s/e/a/ makes the output sad. :(


Other solution (10 bytes):

sort $0
a=

(no trailing newline)

Prints:

a=
sort $0
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Ah, that's really nice. \$\endgroup\$ May 18, 2017 at 3:48
3
\$\begingroup\$

OCaml, 22 bytes

List.find ((=) "") []

Outputs

Exception: Not_found.

It search for "" (empty string) in the empty list []

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to PPCG! It seems this prints a trailing linefeed, so the output is actually 23 bytes. (I don't know OCaml but I imagine the easiest fix would be to pad the code with another space, or add a trailing linefeed to it as well.) \$\endgroup\$ May 18, 2017 at 16:07
  • \$\begingroup\$ Hi, the output is 22 bytes when counting the newline ! However, for the code, I added the trailing newline to the count \$\endgroup\$
    – juloo65
    May 18, 2017 at 16:12
  • 1
    \$\begingroup\$ Ah okay, should have counted the bytes then. It might be good if you mentioned that both code and output include the linefeed. \$\endgroup\$ May 18, 2017 at 16:14
3
\$\begingroup\$

Ruby, 3 bytes

p""

Prints this, plus a newline

""

Here's another 3 byte one:

p:a

Prints this, plus a newline

:a
\$\endgroup\$
3
\$\begingroup\$

R, 7 bytes

stop( )

Prints Error: (with a trailing space)


16 bytes (only works in version 3.3.1)

version$nickname

Prints Bug in Your Hair.

Not nearly as good but I like it anyway.

\$\endgroup\$
3
\$\begingroup\$

charcoal, 1

Try it online!

Explanation:

⎚ Clears the empty screen
[implicitly print nothing plus a trailing newline]
\$\endgroup\$
2
  • \$\begingroup\$ What is that character supposed to be? Is it an old CRT TV screen? \$\endgroup\$
    – Beta Decay
    May 22, 2017 at 8:47
  • \$\begingroup\$ searching it comes up with the wikipedia article for clear, like in bash \$\endgroup\$ May 22, 2017 at 10:07
3
\$\begingroup\$

Mathematica, 2 bytes

Code:

.0

Output:

0.

This is the output of Wolfram kernel from command line, and the plaintext output from the front end. If you must argue about the extra number tick added when copying directly from the front end, then 0.0 will do.

\$\endgroup\$
0
3
\$\begingroup\$

><>, 11 bytes

01+:b=?;:n!

Prints numbers from 1-10 (12345678910)

\$\endgroup\$
4
  • \$\begingroup\$ Nope. Maybe because there's a bounty on it? One of only three featured questions. \$\endgroup\$
    – AGourd
    May 24, 2017 at 19:26
  • \$\begingroup\$ Maybe. Still, not many people are constantly looking at the fifth page. Then again, maybe the OP and bounty-watchers... \$\endgroup\$
    – MD XF
    May 24, 2017 at 19:26
  • \$\begingroup\$ Well, I don't really know what to tell you. I'm as surprised as you honestly. \$\endgroup\$
    – AGourd
    May 24, 2017 at 19:28
  • \$\begingroup\$ I was pretty surprised when my 19-byte solution to this challenge got 100+ votes. And it only took 30 seconds to write... \$\endgroup\$
    – MD XF
    May 24, 2017 at 19:29
3
\$\begingroup\$

Whitespace, 36 bytes

  	 				

  
 
 	
 	   	
	    
 
		

Try it online!

Generates the output "-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1" (36 characters without quotes, no trailing newline)

Explanation

Pushes the number -15 onto the stack (encoded in binary as 01111, with a leading 0 for padding to match the output) then counts toward 0 outputting the current number each iteration.

Starting from a negative number gives an extra byte of output per iteration and also allows me to use a jump while negative instruction to fall through the loop and exit implicitly. That single padding byte in the code is a downer though but solutions without it are longer.

\$\endgroup\$
3
\$\begingroup\$

Python 3, 13 bytes

print("a"*13)

Outputs "aaaaaaaaaaaaa"

\$\endgroup\$
3
\$\begingroup\$

C#, 84 bytes

public class P{public static void Main(){System.Console.Write(new string('_',84));}}

Try Online

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

Bash, 6 bytes

(arch)

Try it online!

The architecture needs to be x86_64

\$\endgroup\$
3
\$\begingroup\$

Klein 110, 7 5 bytes

Thanks to Martin Ender for saving two bytes!

1.
2@

Try it online!

If we unfold the topology here we get

1.122@

This outputs 1 1 2 2 with a trailing newline.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This was valid when the flags were being counted as bytes, I guess? I think it shouldn't be counted either way though, since the interpreter doesn't run without the topology argument; in that case, a variation works at 4 bytes. \$\endgroup\$
    – Bubbler
    Oct 18, 2021 at 5:27
3
\$\begingroup\$

Lean Mean Bean Machine, 28 bytes

 /O
)O4
?7/
 *
_~
 o
~ :
  u

Try it Online!

Outputs 27 1s followed by a trailing newline.

Unfortunately I had to fix a bug in LMBM with the o peg for this answer to work. The link above links to the entire LMBM interpreter, with the fix, contained in TIO's header/footer, with the LMBM code in the code section.

Explanation

The code is a loop. Here's the setup:

 /O
 O4
 7/
 *
 ~

This creates 2 marbles, sets one to 4 and the other to 7, then multiplies them into a single marble with a value of 28. Finally the ~ pushes the marble up to the top of that column, where the top / pushes it into the loop.

)
?

_
 o
~ :
  u

This is the loop. ? is a conditional that sets the marble's spin to right if its value is truthy (non-zero), and 0 otherwise.

) decrements the marble's value. We do this before the conditional to account for LMBM's trailing newline.

_ pushes the marble in the direction of its spin. This is the exit condition for the loop. If the marble's value was not truthy, ie 0, this pushes the marble to the left, out of bounds. Out of bounds marbles are automatically destroyed. When all marbles are destroyed, the program terminates.

o is a split. It splits the marble, pushing one copy left and the other right. The left marble hits ~, which pushes it back up to the top of the loop.

The right marble hits :, which sets its value to its spin (1 for right, 0 for left), then it falls into u, which prints the marble's value and destroys it.

\$\endgroup\$
3
\$\begingroup\$

experimental-type-lang (63 bytes)

Here's a solution in a weird (and not very good) language I made:

type R<E,C>=C extends63?E:R<[...E,32],[...C,_]>;[51224,R<[],0>]

Output:

0> "                                                               "

I'm not counting the 0> output prefix and quotes as part of the program output in this case (the quotes appear whenever the output is a string, and the output prefix is added for every evaluated expression), but if I wanted to count that I could just adjust the value 63 to be the desired output string length.

Explanation

Here's an expanded version:

type Loop<Result = [], Counter = 0> = Counter extends 63
    ? Result
    : Loop<[...Result, 32], [...Counter, _]>;

// The evaluated expression
[51224, Loop<>];

In this language, strings are arrays where the first item is a magic number indicating to the evaluator that it should be displayed as a string, and the second item is an array of character codes. The Loop function (called a type because I wanted to make the syntax resemble the TypeScript type system) is responsible for generating this array of characters. It does this by recursively calling itself, adding 32 (the code for a space) to a result array with the spread operator, until a counter reaches the desired output length. In experimental-type-lang, numbers are just arrays of "items" (represented by a _) with the length being the numerical value; because there is no language-level add operator and importing the Add type from the standard library would take too many characters, I increment the counter by spreading it into an array and adding an _.

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

Cjam, 1 byte

N

Explanation

N e#Push '\n' and implicit print
\$\endgroup\$
2
\$\begingroup\$

J, 6 bytes

echo!4

Try it online!

Output:

24
   

Notice the three spaces on the second line.

\$\endgroup\$
1 2
3
4 5
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.