Self-running source

From vegard.wiki
Revision as of 08:51, 13 March 2020 by Vegard (talk | contribs) (new page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

C

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

#if 0
gcc -Wall $0 && exec ./a.out; exit
#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