Self-running source: Difference between revisions

From vegard.wiki
Jump to navigation Jump to search
Content added Content deleted
(add category)
No edit summary
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
<source lang="C">
<source lang="C">
#if 0
#if 0
gcc -Wall $0 && exec ./a.out; exit
gcc -Wall $0 && exec ./a.out; exit 1
#endif
#endif


Line 27: Line 27:
* https://coderwall.com/p/e1htcg/self-compiling-source-code
* https://coderwall.com/p/e1htcg/self-compiling-source-code
* https://twitter.com/dannas_/status/1238066416286400512
* https://twitter.com/dannas_/status/1238066416286400512
* https://twitter.com/_monoid/status/1264858552440586248


[[Category:C]]
[[Category:C]]

Latest revision as of 11:24, 25 May 2020

C

Set the executable bit on the following polyglot C/shell source file:

#if 0
gcc -Wall $0 && exec ./a.out; exit 1
#endif

#include <stdio.h>

int main(int argc, char *argv[])
{
        printf("Hello world!\n");
        return 0;
}

This is my favourite version because:

  • readable (e.g. no multiline comments)
  • minimal boilerplate
  • only runs the program if it compiled successfully
  • exits with 1 if there was an error

See also