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

240 Answers 240

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

Python 3 REPL, 3 bytes

1e0

Prints 1.0.

If trailing newline counts,

5*2

Prints 10, then a trailing newline.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Yes, the trailing linefeed counts. \$\endgroup\$ – Martin Ender May 18 '17 at 4:36
2
\$\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
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Ah, that's really nice. \$\endgroup\$ – Steve Bennett May 18 '17 at 3:48
2
\$\begingroup\$

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.

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Japt, 1 byte

Outputs a string containing a single ".

Q

Try it online


Or this will output a string containing a single \n.

R

Try it online

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Heh, nice. S will work too, or T, or U, or a, or f, or... there are probably 40 or so options that print 0 or 1. \$\endgroup\$ – ETHproductions May 19 '17 at 0:46
  • \$\begingroup\$ @ETHproductions, T, U, a, f, etc. wouldn't be valid as string output is required. s works, though. \$\endgroup\$ – Shaggy May 19 '17 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 as 123) \$\endgroup\$ – ETHproductions May 19 '17 at 12:02
  • \$\begingroup\$ Unless, in the case of Japt, you use the -Q flag ;) But that's good to know, thanks @ETHproductions. \$\endgroup\$ – Shaggy May 19 '17 at 12:04
2
\$\begingroup\$

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                                       .

Try it online!

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Carrot, 4 bytes

.^*3

Prints .... (4 .s).

Try it online!

Explanation

.^                        Sets the stack-string to "."
  *3                      Append three duplicates of the stack-string to itself
                          Implicit output
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Looks good to me. Welcome to PPCG! :) \$\endgroup\$ – Martin Ender May 23 '17 at 8:57
  • \$\begingroup\$ p (1..4).to_a should work, for 13 bytes. \$\endgroup\$ – Dennis May 23 '17 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\$ – b_jonas Jun 13 '17 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 Jun 13 '17 at 20:15
2
\$\begingroup\$

ZX Spectrum BASIC, 1 byte

PRINT

(which is 1 byte) prints a single newline.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I think Apple][ integer Basic does a similar thing with ? for PRINT \$\endgroup\$ – roblogic Aug 15 '19 at 2:02
2
\$\begingroup\$

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 :)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! You're a little bit outgolfed already. \$\endgroup\$ – Ørjan Johansen May 23 '17 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 May 23 '17 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 May 23 '17 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 May 23 '17 at 18:00
  • 1
    \$\begingroup\$ So basically it needs an emulator. \$\endgroup\$ – Ørjan Johansen May 23 '17 at 18:03
2
\$\begingroup\$

Microscript, 1 byte

E

Prints 100=1. e will also work, printing 20=1.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

SmileBASIC 3, 3 bytes

?@A

Prints the string @A, plus a trailing newline, so output is 3 bytes.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

MarioLANG, 1 byte

:

Output the numeric value from current memory cell, which is 0

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
  • \$\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 Jul 11 '17 at 1:36
  • \$\begingroup\$ @lirtosiast You could always have something like Repeat 1:End for 4 bytes of course. \$\endgroup\$ – Timtech Jul 11 '17 at 2:21
  • \$\begingroup\$ Yes, that would be better \$\endgroup\$ – lirtosiast Jul 11 '17 at 16:19
2
\$\begingroup\$

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?

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Python 3 REPL, 11 bytes

str(...)[:]

Try it online! The header and the footer emulate a REPL.

This prints:

'Ellipsis'

| improve this answer | |
\$\endgroup\$
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::*

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

><>, 3 bytes

"nh

Try It Online

Prints "104" and exits with an error.

| improve this answer | |
\$\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.

| improve this 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)

| improve this answer | |
\$\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.

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

brainfuck, 15 32 bytes

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

Try it online!

Illegal version (no ascii), 15 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\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 '19 at 10:47
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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

C#, 84 bytes

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

Try Online

| improve this answer | |
\$\endgroup\$
2
\$\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)

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Actually, 2 bytes

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

1╤

Try it online!

| improve this answer | |
\$\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 🄂.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I don't see that character in the codepage for the language. What byte value does it have? \$\endgroup\$ – Jo King Oct 14 '19 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 '19 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 '19 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 '19 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
| improve this answer | |
\$\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

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

MAWP, 1 bytes

:

Try it!

Prints the 1 thats already on stack

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ A fun but longer solution is M:. \$\endgroup\$ – Jo King Aug 8 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 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 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 at 18:36
1 2 3
4
5
8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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