Funny Code

There are 10 kinds of people in this world: those who understand binary, those who don’t.

This is my collection of funny and interesting code.

Hello World

Hello World is a classical masterpiece in computer science Wikipedia. There exists an HelloWorld code snippet nearly for any programming language Rosetta Code.

main(){puts("HelloWorld");}
public class HelloWorld {
  public static void main(Sring[] args) {
    System.out.println("HelloWorld");
  }
}

Answer to the Ultimate Question of Life, the Universe, and Everything

main(){return'*';}

Russian Roulette

My shortest, fully functional, game.

main(){puts(time()%6?"click...":"\7BANG!");}

Funny expressions

// no comment
delete this;
long long ago;
while(!asleep)sheeps++;

greatest power of two that is a divisor of n

return n & -n;

integer swap

a^=b^=a^=b;

Null-termined string copy

while((*dst++ = *src++) != '\0');

Random numbers

n*=0x9e3779b1;

Null compiler

main(){while(getchar()>0);}

Yes utilty

main(){while(puts("y"));}

PID hopper

main(){while(!fork())sleep(1);}

Fork bomb

main(){while(1)fork();}

Quine

main(a){printf(a="main(a){printf(a=%c%s%c,34,a,34);}",34,a,34);}

Memory eater

main(){while(malloc(1000));}

Prime finder

main(x,y){for(;x++;)for(y=2;x%y;)printf(++y/x+"\0%d\n",x);}

to convert prefix to postfix

main() {  char c = getchar();  (c == '+' || c == '-' || c == '*' || c == '/') ? main(), main() : 0;  putchar(c);}

Compute 2880 digits of PI

g,o,p,i=1e4,a[10001];main(x){for(;p?g=g/x*p+a[p]*i+2*!o:
53^(printf("%.4d",o+g/i),p=i,o=g%i);a[p--]=g%x)x=p*2-1;}

Parrot

main(){while(putchar(getchar()));}

References