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
C (modern Linux), 19 bytes
main(){puts('s');}
When compiled and run, this prints:
Segmentation fault
-
24\$\begingroup\$ This is brilliant :D \$\endgroup\$Beta Decay– Beta Decay2017年05月17日 22:13:05 +00:00Commented May 17, 2017 at 22:13
-
7\$\begingroup\$ this is more like "C + English language unix/posix OS ;)" \$\endgroup\$Florian Castellane– Florian Castellane2017年05月18日 08:35:45 +00:00Commented May 18, 2017 at 8:35
-
7\$\begingroup\$ I think it would be better to write "C, modern Linux" than just "C": Under Windows the error message is different and in old times Linux programs were even compiled in a way that the address 0x73 ('s') was readable so no exception was caused. \$\endgroup\$Martin Rosenau– Martin Rosenau2017年05月18日 14:21:38 +00:00Commented May 18, 2017 at 14:21
-
13\$\begingroup\$ I joined the site just to upvote this answer. \$\endgroup\$Nitish– Nitish2017年05月19日 10:46:11 +00:00Commented May 19, 2017 at 10:46
-
12\$\begingroup\$ As impressive as this is, it's not really the C program that prints the output, but the shell it's running in. \$\endgroup\$Dennis– Dennis2017年05月23日 04:27:40 +00:00Commented May 23, 2017 at 4:27
Excel, 11 bytes
Norwegian language version:
=SMÅ(13^9)
English language version (12 bytes):
=LOWER(17^9)
Generates n-digit number and converts to text by converting to lowercase.
-
132\$\begingroup\$ This gives a whole new meaning to "picking the right language for the job". \$\endgroup\$Martin Ender– Martin Ender2017年05月17日 20:22:40 +00:00Commented May 17, 2017 at 20:22
-
5\$\begingroup\$ This is a great idea. It would be shorter to do
=9^7&""which generates a 7 digit number in either language and is only 7 bytes. \$\endgroup\$OpiesDad– OpiesDad2017年05月18日 22:39:17 +00:00Commented May 18, 2017 at 22:39 -
\$\begingroup\$ @OpiesDad Nice solution, I think you should post it as an answer, so I can upvote. \$\endgroup\$pajonk– pajonk2017年05月19日 05:23:07 +00:00Commented May 19, 2017 at 5:23
-
5\$\begingroup\$ @MartinEnder It amuses me how your speculation/potential joke is more upvoted than this answer (though it's close). \$\endgroup\$2017年05月26日 14:42:42 +00:00Commented May 26, 2017 at 14:42
Bash (builtins only), 8 bytes
{e,,}cho
Prints cho cho and a newline.
-
1\$\begingroup\$ Like a train: choo choo! \$\endgroup\$qwr– qwr2022年02月18日 08:34:01 +00:00Commented Feb 18, 2022 at 8:34
Labyrinth, 4 bytes
!!>@
Prints 0000
Explanation
! Print an implicit 0 from the stack.
! Print an implicit 0 from the stack.
> Rotate the source code right by one cell, so the code now becomes
@!!>
The IP is moved along, so it's now at the end of the line, which is
a dead end. So the IP turns around and starts moving left.
! Print an implicit 0 from the stack.
! Print an implicit 0 from the stack.
@ Terminate the program.
-
8\$\begingroup\$ That is too clever. Have a +1, on the house! \$\endgroup\$2017年05月17日 15:22:03 +00:00Commented May 17, 2017 at 15:22
Retina, 2 bytes
no
Prints 0 and a linefeed.
There are a lot of 2-byte solutions, but I believe this is optimal. Retina by default always prints a trailing newline and getting rid of it takes too many bytes. So we'd have to find a 1-byte program that leaves the empty input unchanged. I believe the only program which does this is the program containing a single linefeed, which is therefore equal to the output and hence not permitted by the challenge.
The next simplest thing to do is to live with Retina outputting a single digit (the number of matches of some regex against the empty input), and we can do that with a lot of failing (or matching) 2-byte patterns.
Mathematica, 2 bytes
4!
factorial
24
-
1\$\begingroup\$ Gah! too clever. \$\endgroup\$tuskiomi– tuskiomi2017年05月17日 18:31:03 +00:00Commented May 17, 2017 at 18:31
-
2\$\begingroup\$ beat me to it! +1. (I think you should mention that this is in the REPL version of Mathematica.) \$\endgroup\$Greg Martin– Greg Martin2017年05月17日 20:18:59 +00:00Commented May 17, 2017 at 20:18
-
\$\begingroup\$ If this is a reasonable answer, wouldn't the answer "1" also be a reasonable answer? (Putting '1' into Mathematica's REPL would return '1' too...) \$\endgroup\$Mark Segal– Mark Segal2017年05月20日 19:18:26 +00:00Commented May 20, 2017 at 19:18
-
4\$\begingroup\$ @MarkSegal That would violate the no-quine-rule. \$\endgroup\$AlexR– AlexR2017年05月20日 19:48:31 +00:00Commented May 20, 2017 at 19:48
-
7\$\begingroup\$ Rules:..."...the code and the output must differ in at least one byte" \$\endgroup\$ZaMoC– ZaMoC2017年05月20日 19:48:49 +00:00Commented May 20, 2017 at 19:48
C, 20 bytes
f(){printf("%20d");}
Outputs some number, padded with spaces to a length of 20. (What number? Whatever happens to come next in memory.)
Some sample runs on my system:
llama@llama:...code/c/ppcg121056samelen$ ./a.out
-666605944
llama@llama:...code/c/ppcg121056samelen$ ./a.out
-1391039592
llama@llama:...code/c/ppcg121056samelen$ ./a.out
1727404696
llama@llama:...code/c/ppcg121056samelen$ ./a.out
10717352
llama@llama:...code/c/ppcg121056samelen$ ./a.out
1485936232
It's a shame that the output can't be arbitrary bytes, because that would have allowed this 19 byte solution:
f(){write(1,f,19);}
which outputs 19 bytes of junk, starting at f's address.
-
\$\begingroup\$ I'm glad to see an answer that makes use of the varying-output rule. :) \$\endgroup\$Martin Ender– Martin Ender2017年05月17日 12:33:08 +00:00Commented May 17, 2017 at 12:33
-
\$\begingroup\$ Couldn't this segfault? \$\endgroup\$Daniel– Daniel2017年05月17日 16:50:56 +00:00Commented May 17, 2017 at 16:50
-
\$\begingroup\$ Where's the TIO link(s)? \$\endgroup\$CalculatorFeline– CalculatorFeline2017年05月17日 19:06:02 +00:00Commented May 17, 2017 at 19:06
-
\$\begingroup\$ @Daniel No, the number printed will come from the previous contents of the
%esiregister. \$\endgroup\$Doorknob– Doorknob2017年05月17日 20:03:19 +00:00Commented May 17, 2017 at 20:03 -
9\$\begingroup\$ @Doorknob but it's still undefined behavior, therefore a more sadistic compiler might do something entirely different here \$\endgroup\$Display Name– Display Name2017年05月18日 07:12:07 +00:00Commented May 18, 2017 at 7:12
Bash on Linux, 6
uname
(followed by a newline)
Outputs Linux followed by a newline.
-
2\$\begingroup\$ Technically
unameisn't a bash builtin - it's a separate executable \$\endgroup\$Jason Musgrove– Jason Musgrove2017年05月25日 11:13:06 +00:00Commented May 25, 2017 at 11:13 -
1\$\begingroup\$ -1, and actually a builtin \$\endgroup\$GammaFunction– GammaFunction2019年08月18日 03:12:56 +00:00Commented Aug 18, 2019 at 3:12
-
\$\begingroup\$ @GammaFunction Where do you see
unameas a bash builtin? Every distro I can lay my hands on right now it is a separate binary, including TIO. Conversely, as your linked answer shows,umaskis a builtin in most/all shells. \$\endgroup\$Digital Trauma– Digital Trauma2019年08月21日 16:27:23 +00:00Commented Aug 21, 2019 at 16:27 -
1\$\begingroup\$ I don't see
unameas a builtin. When I said "-1, and actually a builtin", I meant "This answer usesumaskfor -1 bytes, andumaskis actually a builtin". \$\endgroup\$GammaFunction– GammaFunction2019年08月21日 16:30:18 +00:00Commented Aug 21, 2019 at 16:30 -
1\$\begingroup\$ @GammaFunction Sorry - I misunderstood the meaning of the "-1" in your comment - I'm used to those meaning downvote and a comment explaining why. \$\endgroup\$Digital Trauma– Digital Trauma2019年08月21日 17:00:30 +00:00Commented Aug 21, 2019 at 17:00
Javascript ES6, 9 bytes
Using Template Strings
_=>`${_}`
f=
_=>`${_}`
console.log(f());
console.log(typeof f());
-
\$\begingroup\$ Is
undefinedconsidered a string? \$\endgroup\$Shaggy– Shaggy2017年05月17日 11:44:51 +00:00Commented May 17, 2017 at 11:44 -
3\$\begingroup\$ @Shaggy See the edit. I am using String interpolation \$\endgroup\$Weedoze– Weedoze2017年05月17日 11:46:20 +00:00Commented May 17, 2017 at 11:46
-
\$\begingroup\$ Wow this is my second post on PPCG and 14 upvotes ! Thanks \$\endgroup\$Weedoze– Weedoze2017年05月18日 06:03:54 +00:00Commented May 18, 2017 at 6:03
-
1\$\begingroup\$ Why simple whey you can have it difficult?
(_=>_+''). \$\endgroup\$GOTO 0– GOTO 02017年05月18日 09:46:08 +00:00Commented May 18, 2017 at 9:46 -
1\$\begingroup\$ @GOTO0 Oh ok you keep the parenthesis. This is another option. I personally prefer mine hehe \$\endgroup\$Weedoze– Weedoze2017年05月18日 09:56:44 +00:00Commented May 18, 2017 at 9:56
Pyramid Scheme, (削除) 74 (削除ここまで) (削除) 43 (削除ここまで) 42 bytes
Saved 31 bytes thanks to Khuldraeseth na'Barya! Saved 1 byte thanks to JoKing's redesigned solution!
^
/^\
^---^
-^ ^-
-^-
/2\
/ 8 \
-----
Try it online! Outputs the 41-digit number 28^28 =わ 33145523113253374862572728253364605812736, followed by a trailing newline.
Old version
^
/ \
/out\
-----^
/^\
^---^
/1\ /9\
/606\---
/51015\
-------
Outputs 71277303925397560663333806233294794013421332605135474842607729452115234375 = 160651015 ** 9, or about 1074.
-
10\$\begingroup\$ Hey, are you trying to sell your language? If so, you're under arrest. -- The Cops \$\endgroup\$NoOneIsHere– NoOneIsHere2017年07月21日 21:58:55 +00:00Commented Jul 21, 2017 at 21:58
-
2\$\begingroup\$ @NoOneIsHere pardon? \$\endgroup\$Conor O'Brien– Conor O'Brien2017年07月21日 23:46:10 +00:00Commented Jul 21, 2017 at 23:46
-
3\$\begingroup\$ Sorry, it's called Pyramid Scheme, which is also the name of a type of scam. \$\endgroup\$NoOneIsHere– NoOneIsHere2017年07月22日 01:47:42 +00:00Commented Jul 22, 2017 at 1:47
-
1\$\begingroup\$ ohhhhhhhhhhhh rofl \$\endgroup\$Conor O'Brien– Conor O'Brien2017年07月22日 02:42:57 +00:00Commented Jul 22, 2017 at 2:42
-
3\$\begingroup\$ Implicit output gets you down to 43 bytes. \$\endgroup\$Khuldraeseth na'Barya– Khuldraeseth na'Barya2019年07月25日 19:00:53 +00:00Commented Jul 25, 2019 at 19:00
-
15\$\begingroup\$ From brute-forcing programs starting with
print, there are no shorter solutions, and the only 9-bytes ones are variants of this and Luis Mendo'sprint 1e5. \$\endgroup\$xnor– xnor2017年05月17日 20:56:25 +00:00Commented May 17, 2017 at 20:56
-
\$\begingroup\$ Do trailing newlines count as a character as far as output? Otherwise, this prints eight bytes. \$\endgroup\$AAM111– AAM1112017年05月18日 00:10:10 +00:00Commented May 18, 2017 at 0:10
-
\$\begingroup\$ @OldBunny2800 Yes, the challenge says If your output contains trailing newlines, those are part of the byte count. Anyway, I'll clarify that in my answer \$\endgroup\$Luis Mendo– Luis Mendo2017年05月18日 00:18:41 +00:00Commented May 18, 2017 at 0:18
brainfuck, 25 bytes
--[-->+<]+++++[->-.....<]
Try it online! Note: Requires an implementation with 8-bit unsigned cells
Output:
~~~~~}}}}}|||||{{{{{zzzzz
Explanation
--[ 254
-->+<] /2 = 127 into the second cell
+++++[ Five times
->-.....<] Print the second cell - 1 five times
-
4\$\begingroup\$ There are many BF-variations like a 8-bit-cell, infite tape. or a limited tape and infinite cells. I'm pretty sure yours is a unsigned 8-bit cell model, but i think it'd be good if you include that into your answer. \$\endgroup\$Linnea Gräf– Linnea Gräf2017年05月17日 14:07:10 +00:00Commented May 17, 2017 at 14:07
-
\$\begingroup\$ @RomanGräf Good point! I usually do include that information if a non-standard implementation is required. This answer should work on any standard implementation (8-bit unsigned cells, any tape), but I'll add a note to clarify. \$\endgroup\$Zack C.– Zack C.2017年05月17日 15:28:09 +00:00Commented May 17, 2017 at 15:28
-
\$\begingroup\$ Could you not have a 1 byte program
.which prints the unprintableNULLcharacter? \$\endgroup\$Graviton– Graviton2017年05月17日 21:03:39 +00:00Commented May 17, 2017 at 21:03 -
\$\begingroup\$ @Graviton That would violate the first rule of this challenge. However, that's a valid solution in a BF derivative that defaults to a printable value (see: my brainbool answer). \$\endgroup\$Zack C.– Zack C.2017年05月17日 21:33:57 +00:00Commented May 17, 2017 at 21:33
-
C (Ideone), 14 bytes
f(){warn(0);}
On Ideone, which names its executable prog, this outputs the following with a trailing newline.
prog: Success
C (GCC), 15 bytes
f(){warn(00);}
Because GCC writes an executable named a.out by default (in the absence of additional flags that would cost bytes), this outputs the following with a trailing newline.
a.out: Success
-
7\$\begingroup\$ @MDXF The spec says program or function, so a function submission is perfectly valid. Since the filename isn't chosen by the user here (both Ideone and gcc have defaults), I think this is fine (personal opinion). \$\endgroup\$Dennis– Dennis2017年05月26日 00:00:31 +00:00Commented May 26, 2017 at 0:00
Self-modifying Brainfuck, 5 bytes
<[.<]
Output:
]<.[<
Explanation:
Really simple, prints the source in reverse. In SMBF, the content of the program is stored on the tape, to the left of the initial position of the pointer. Gliding left and printing will output the source code backwards.
Since reading source is allowed in this challenge, this should definitely be within the rules.
Basic Arithmetic Calculator, 2 bytes
1=
prints 1., or:
|
|
| .
on those silly seven-segment displays.
To reproduce, pick up any random calculator; they all have this programming language installed somehow.
-
1\$\begingroup\$ Is this true for all calculators? I'm sure they do constantly show the decimal point \$\endgroup\$Beta Decay– Beta Decay2017年05月18日 08:55:27 +00:00Commented May 18, 2017 at 8:55
-
\$\begingroup\$ Isn't it
1.instead of1x? Also this is a quine, which is disallowed by the rules. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月18日 16:42:57 +00:00Commented May 18, 2017 at 16:42 -
\$\begingroup\$ No, it's
1=, I messed up. I meant1xthan implicit equals, but I forgot that=is a button. \$\endgroup\$AAM111– AAM1112017年05月18日 18:48:13 +00:00Commented May 18, 2017 at 18:48 -
\$\begingroup\$ It only outputs
1on both my TI-36 and TI-84 CE. Do these not count as "basic" calculators? \$\endgroup\$Benjamin Urquhart– Benjamin Urquhart2019年07月31日 18:58:47 +00:00Commented Jul 31, 2019 at 18:58 -
\$\begingroup\$ No, a TI 84 is not an arithmetic calculator, it’s a graphing calculator. I don’t know about the 36, but I’m pretty sure it’s a scientific calculator. This answer is for four-function calculators. \$\endgroup\$AAM111– AAM1112019年08月01日 15:14:45 +00:00Commented Aug 1, 2019 at 15:14
C (gcc), (削除) 18 (削除ここまで) 17 bytes
f(){puts('@C');}
Note that there's an STX byte (0x02) between @ and C.
Portability
This has been tested with gcc 6.3.1 and clang 3.9.1 on Fedora 25, gcc 4.8.4 on Ubuntu 14.04.4, and gcc 4.8.3 on openSUSE 13.2, where it prints the following output.
inux-x86-64.so.2
I expect this to produce the same output with all versions of gcc, as long as it compiles to an executable of the following type.
ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2
Different platforms will require a different memory address and possibly a different order for the bytes in the multi-character character constant.
For example, replacing @2円C with @2円4円 prints exec/ld-elf.so.1 and a newline on FreeBSD 11 with clang 3.8.0.
Offline verification
$ printf "%b\n" "f(){puts('@2円C');}main(){f();}" > quine.c
$ gcc -w -o quine quine.c
$ ./quine
inux-x86-64.so.2
$ ./quine | wc -c
17
How it works
By default, ld uses 0x400000 as the base address of the text segment, meaning that we can find the ELF's content starting at memory address 0x400000.
The first 640 bytes of the ELF are largely independent of the actual source code. For example, if the declaration of f is followed by main(){f();} and nothing else, they look as follows.
00000000: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 .ELF............
00000010: 02 00 3e 00 01 00 00 00 00 04 40 00 00 00 00 00 ..>.......@.....
00000020: 40 00 00 00 00 00 00 00 e8 19 00 00 00 00 00 00 @...............
00000030: 00 00 00 00 40 00 38 00 09 00 40 00 1e 00 1b 00 [email protected]...@.....
00000040: 06 00 00 00 05 00 00 00 40 00 00 00 00 00 00 00 ........@.......
00000050: 40 00 40 00 00 00 00 00 40 00 40 00 00 00 00 00 @.@.....@.@.....
00000060: f8 01 00 00 00 00 00 00 f8 01 00 00 00 00 00 00 ................
00000070: 08 00 00 00 00 00 00 00 03 00 00 00 04 00 00 00 ................
00000080: 38 02 00 00 00 00 00 00 38 02 40 00 00 00 00 00 8.......8.@.....
00000090: 38 02 40 00 00 00 00 00 1c 00 00 00 00 00 00 00 8.@.............
000000a0: 1c 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 ................
000000b0: 01 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 ................
000000c0: 00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 ..@.......@.....
000000d0: 04 07 00 00 00 00 00 00 04 07 00 00 00 00 00 00 ................
000000e0: 00 00 20 00 00 00 00 00 01 00 00 00 06 00 00 00 .. .............
000000f0: 08 0e 00 00 00 00 00 00 08 0e 60 00 00 00 00 00 ..........`.....
00000100: 08 0e 60 00 00 00 00 00 1c 02 00 00 00 00 00 00 ..`.............
00000110: 20 02 00 00 00 00 00 00 00 00 20 00 00 00 00 00 ......... .....
00000120: 02 00 00 00 06 00 00 00 20 0e 00 00 00 00 00 00 ........ .......
00000130: 20 0e 60 00 00 00 00 00 20 0e 60 00 00 00 00 00 .`..... .`.....
00000140: d0 01 00 00 00 00 00 00 d0 01 00 00 00 00 00 00 ................
00000150: 08 00 00 00 00 00 00 00 04 00 00 00 04 00 00 00 ................
00000160: 54 02 00 00 00 00 00 00 54 02 40 00 00 00 00 00 T.......T.@.....
00000170: 54 02 40 00 00 00 00 00 44 00 00 00 00 00 00 00 [email protected].......
00000180: 44 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 D...............
00000190: 50 e5 74 64 04 00 00 00 b0 05 00 00 00 00 00 00 P.td............
000001a0: b0 05 40 00 00 00 00 00 b0 05 40 00 00 00 00 00 ..@.......@.....
000001b0: 3c 00 00 00 00 00 00 00 3c 00 00 00 00 00 00 00 <.......<.......
000001c0: 04 00 00 00 00 00 00 00 51 e5 74 64 06 00 00 00 ........Q.td....
000001d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001f0: 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 ................
00000200: 52 e5 74 64 04 00 00 00 08 0e 00 00 00 00 00 00 R.td............
00000210: 08 0e 60 00 00 00 00 00 08 0e 60 00 00 00 00 00 ..`.......`.....
00000220: f8 01 00 00 00 00 00 00 f8 01 00 00 00 00 00 00 ................
00000230: 01 00 00 00 00 00 00 00 2f 6c 69 62 36 34 2f 6c ......../lib64/l
00000240: 64 2d 6c 69 6e 75 78 2d 78 38 36 2d 36 34 2e 73 d-linux-x86-64.s
00000250: 6f 2e 32 00 04 00 00 00 10 00 00 00 01 00 00 00 o.2.............
00000260: 47 4e 55 00 00 00 00 00 02 00 00 00 06 00 00 00 GNU.............
00000270: 20 00 00 00 04 00 00 00 14 00 00 00 03 00 00 00 ...............
Using, e.g., main(int c, char**v){f();} instead changes some bytes, but not the offset of the string /lib64/ld-linux-x86-64.so.2, which we'll use to produce output.
The offset of said string is 0x238 and it is 27 bytes long. We only want to print 17 bytes (and the last one will be a newline if we use puts), so we add 11 to the offset to get 0x243, the offset of inux-x86-64.so.2. Adding 0x400000 and 0x243 gives 0x400243, the memory location of inux-x86-64.so.2.
To obtain this memory address, we can use multi-character character constants, which exhibit implementation-defined behavior. 0x400243 is (64)(2)(67) in base 256 and gcc's multi-character character constants use big-endian byte order, so '@2円C' yields the memory address of the desired string.
Finally, puts prints the (null-terminated) sting at that memory location and a trailing newline, creating 17 bytes of output.
-
\$\begingroup\$ This is wizardry... does this assume the byte order of the compiled assembly or something? \$\endgroup\$Patrick Roberts– Patrick Roberts2017年05月23日 09:49:24 +00:00Commented May 23, 2017 at 9:49
-
\$\begingroup\$ Output on my system (Linux with GCC 6.3.1):
Uwhen compiling with-Df=main.Uwhen compiling withmain(){f();}. \$\endgroup\$MD XF– MD XF2017年05月23日 14:56:19 +00:00Commented May 23, 2017 at 14:56 -
\$\begingroup\$ @PatrickRoberts At this point, I'm not really sure what the assumptions are. Since it pokes around in the assembled ELF file, it pretty much depends on everything the compiler writes before the actual code. \$\endgroup\$Dennis– Dennis2017年05月23日 15:26:10 +00:00Commented May 23, 2017 at 15:26
-
\$\begingroup\$ @MDXF Found a shorter way to get a suitable memory address. It works on my Ubuntu 14.04 VPS now, but ymmv. \$\endgroup\$Dennis– Dennis2017年05月23日 17:09:50 +00:00Commented May 23, 2017 at 17:09
-
\$\begingroup\$ @Dennis Works fine now (dang it). Bounty is yours if by the end of the week no one finds a shorter solution. \$\endgroup\$MD XF– MD XF2017年05月23日 18:02:51 +00:00Commented May 23, 2017 at 18:02
Fourier, (削除) 26 (削除ここまで) (削除) 22 (削除ここまで) 20 bytes
5^(`na`&i)` Batman!`
Outputs:
nananananana Batman!
For proper capitalisation, it's 4 extra bytes:
`N`7^(`an`i^~i)`a Batman!`
Nanananananananana Batman!
-
1\$\begingroup\$ You don't have to include NC (only if you made string printing for this challenge (and that would be silly) \$\endgroup\$user63187– user631872017年09月18日 22:31:56 +00:00Commented Sep 18, 2017 at 22:31
-
\$\begingroup\$ @Christopher Huh, I didn't know that \$\endgroup\$Beta Decay– Beta Decay2017年09月19日 06:21:53 +00:00Commented Sep 19, 2017 at 6:21
Brachylog, 1 byte
w
Explanation
w is the built-in "write". Here, it will write the Input. Since the Input is a free variable, w will label it as an integer before printing. The first integer it tries is 0.
-
\$\begingroup\$
Here, it will write the Input.Is 'the Input' the program itself? Doesn't this count as reading the source, which is by default cheating in a quine? \$\endgroup\$MD XF– MD XF2017年05月17日 21:58:06 +00:00Commented May 17, 2017 at 21:58 -
4\$\begingroup\$ @MDXF "Usual quine rules don't apply" \$\endgroup\$Rob Watts– Rob Watts2017年05月17日 22:48:37 +00:00Commented May 17, 2017 at 22:48
-
3\$\begingroup\$ @MDXF The Input is a special Brachylog variable that you can set when calling a Brachylog program. It goes into the Input part of TryItOnline. Here we don't set anything to the Input, so it is effectively a variable. It is not the program. \$\endgroup\$Fatalize– Fatalize2017年05月18日 07:25:56 +00:00Commented May 18, 2017 at 7:25
Java (OpenJDK 8), 11 bytes
o->1e8-1+""
Output:
9.9999999E7
Just a tad more elaborate than the obvious answer, ()->"".format("%23s",0).
Saves
- 18 -> 16 bytes: More advantageous combination of rounding and power of 10, thanks to PunPun1000
- 16 -> 13 bytes: better formula, thanks to JollyJoker
- 13 -> 11 bytes: improved formula, thanks to Kevin Cruijssen
-
2\$\begingroup\$
()->1E10-1+"","9.999999999E9" (13)saves three more bytes \$\endgroup\$JollyJoker– JollyJoker2017年05月23日 10:29:42 +00:00Commented May 23, 2017 at 10:29 -
1\$\begingroup\$ You can save 1 byte by changing
1e10to1e9, or 2 bytes by using an empty input instead of()like this:o->1e8-1+""(outputs9.9999999E7; length & byte-count 11). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年09月18日 12:09:04 +00:00Commented Sep 18, 2017 at 12:09
05AB1E, 1 byte
õ
Outputs a single newline. õ pushes an empty string, and it is implicitly outputted with a newline.
Some other 2 byte solutions, for your viewing pleasure (the output is in the brackets, and all output has a trailing newline):
X, (1)
Y, (2)
3⁄4, (0)
1⁄4, (1)
¶, (newline)
ð, (space)
Î, (0)
There are way more 2 byte solutions though.
-
\$\begingroup\$ "which is an accepted output method" only for function solutions, not for full programs. \$\endgroup\$Martin Ender– Martin Ender2017年05月17日 11:30:04 +00:00Commented May 17, 2017 at 11:30
-
\$\begingroup\$ @MartinEnder Would those solutions count as functions, then? Not really sure, as I don't often use functions here. \$\endgroup\$Okx– Okx2017年05月17日 11:32:01 +00:00Commented May 17, 2017 at 11:32
-
\$\begingroup\$ I don't know enough about 05AB1E but they're only considered functions if they are actually expressions that evaluate to a function object (which I doubt they do since using them immediately causes the command to be executed). \$\endgroup\$Martin Ender– Martin Ender2017年05月17日 11:33:40 +00:00Commented May 17, 2017 at 11:33
Batch, 12 bytes
@echo %OS%
Byte count includes trailing newline for both script and output, which is
Windows_NT
-
\$\begingroup\$ It seems that it's 11 bytes not 12. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月24日 13:46:58 +00:00Commented May 24, 2017 at 13:46
-
\$\begingroup\$ @EriktheOutgolfer
cmd.exewill of course echo a CRLF after theWindows_NT, thus 12 bytes. \$\endgroup\$Neil– Neil2017年05月24日 14:35:51 +00:00Commented May 24, 2017 at 14:35 -
\$\begingroup\$ Then you should clarify? \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年05月24日 14:36:40 +00:00Commented May 24, 2017 at 14:36
Hexagony, 3 bytes
o!@
Prints 111.
Unfolded:
o !
@ . .
. .
But the code is really just run in the order o!@.
o Set the memory edge to 111, the code point of 'o'.
! Print this value as a decimal integer.
@ Terminate the program.
-
\$\begingroup\$ I'm assuming this isn't using the basic ! to just print a 0? \$\endgroup\$Underslash– Underslash2020年11月03日 22:14:42 +00:00Commented Nov 3, 2020 at 22:14
-
\$\begingroup\$ @Underslash A program without
@wouldn't terminate. \$\endgroup\$Martin Ender– Martin Ender2020年11月04日 09:17:09 +00:00Commented Nov 4, 2020 at 9:17 -
\$\begingroup\$ Oh right I am dumb. I think I thought that it would just run forever, but then I realized that it would just loop back to the 1st line instead of the last one. \$\endgroup\$Underslash– Underslash2020年11月05日 00:53:29 +00:00Commented Nov 5, 2020 at 0:53
-
\$\begingroup\$ @Underslash In a 1-byte program there isn't even a "last line", because that fits in a 1x1x1 hexagon, so a 1-byte program just repeatedly executes its only command, whatever that is (unless it terminates due to
@or a division-by-zero error). \$\endgroup\$Martin Ender– Martin Ender2020年11月11日 21:13:58 +00:00Commented Nov 11, 2020 at 21:13
V/vim, 1 byte
o
This prints a single newline.
There are a bunch of variants on this that would work too. For example,
O
in vim, and
Ä
ä
ï
Ï
in V.
There are also many many many three byte solutions. For example:
3ii
i3i
¬ac
These are all specific to V.
-
\$\begingroup\$ 3ii doesn't expand until you press escape, so should that be three or four bytes (or keystrokes, rather)? \$\endgroup\$algmyr– algmyr2017年05月18日 04:43:40 +00:00Commented May 18, 2017 at 4:43
-
\$\begingroup\$ @algmyr In vim, you are correct. It would have to be
4ii<ESC>However V implicitly fills in the escape at the end of the program. \$\endgroup\$DJMcMayhem– DJMcMayhem2017年05月18日 05:29:39 +00:00Commented May 18, 2017 at 5:29
Well, uh...
Mornington Crescent, 1731 bytes
Take Northern Line to Stockwell
Take Victoria Line to Seven Sisters
Take Victoria Line to Green Park
Take Piccadilly Line to Green Park
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to Russell Square
Take Piccadilly Line to King's Cross St. Pancras
Take Metropolitan Line to King's Cross St. Pancras
Take Hammersmith & City Line to King's Cross St. Pancras
Take Hammersmith & City Line to King's Cross St. Pancras
Take Hammersmith & City Line to King's Cross St. Pancras
Take Hammersmith & City Line to King's Cross St. Pancras
Take Hammersmith & City Line to King's Cross St. Pancras
Take Hammersmith & City Line to King's Cross St. Pancras
Take Hammersmith & City Line to King's Cross St. Pancras
Take Hammersmith & City Line to King's Cross St. Pancras
Take Circle Line to King's Cross St. Pancras
Take Circle Line to King's Cross St. Pancras
Take Circle Line to Bank
Take Circle Line to Bank
Take Northern Line to Mornington Crescent
Prints out the value of 7^2048, which is 1731 digits long. Try it online! Verify that it's actually 1731 digits long!
-
\$\begingroup\$ I like this answer a lot. \$\endgroup\$Razetime– Razetime2020年09月18日 14:45:59 +00:00Commented Sep 18, 2020 at 14:45
///, 12 bytes
/a/bcd/aaaa/
This prints bcdbcdbcdbcd, and because this is 12 bytes, I've added a harmless / to the end of the code to pad it out.
-
\$\begingroup\$ With only (back)slashes, 34 bytes:
/\/\\/\\\\\\\\\\\\\//\/\\\\\\\\\\\\$\endgroup\$tjjfvi– tjjfvi2021年08月07日 22:44:49 +00:00Commented Aug 7, 2021 at 22:44
R, 7 bytes
mode(T)
Prints "logical"
-
\$\begingroup\$ Welcome to PPCG! \$\endgroup\$Martin Ender– Martin Ender2017年06月25日 11:49:28 +00:00Commented Jun 25, 2017 at 11:49
MATL, 2 bytes
H
There is a trailing newline.
Explanation
Clipboard H contains number 2 by default. H pushes that content to the stack, which gets implicitly displayed with a trailing newline.
-
\$\begingroup\$ Man charcoal's a really nifty language, I should learn it some time \$\endgroup\$Mayube– Mayube2017年05月17日 11:59:10 +00:00Commented May 17, 2017 at 11:59