集12个main为一身的C程序竟然也能运行,而且可移植性为100%

今天无意中在网上看到一个C的程序,简单,却深奥。据说是个大师写的,实在是令人感到恐怖。

这样的程序竟然也能运行? 没想到吧 ! 什么事情都有可能发生,而且这样的事情还多着呢?如果你只是被它的结构迷惑,那没关系,不过…… 程序的运行结果可能会让你更加吃惊了。这不得不让我想起“The Tao of Programming”中的一句话“编程大师的程序再凌乱,也用不着测试,因为它是正确的”。

程序的源代码如下:

  1. #include <stdio.h>
  2. main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
  3. main(-86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
  4. main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
  5. "@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
  6. ;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
  7. q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
  8. ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
  9. iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
  10. ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
  11. }'+}##(!!/")
  12. :t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
  13. :0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
  14. "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

我试着在GCC下编译以下,居然通过了。而且成功地输出的结果:

  1. On the first day of Christmas my true love gave to me
  2. a partridge in a pear tree.
  3.  
  4. On the second day of Christmas my true love gave to me
  5. two turtle doves
  6. and a partridge in a pear tree.
  7.  
  8. On the third day of Christmas my true love gave to me
  9. three french hens, two turtle doves
  10. and a partridge in a pear tree.
  11.  
  12. On the fourth day of Christmas my true love gave to me
  13. four calling birds, three french hens, two turtle doves
  14. and a partridge in a pear tree.
  15.  
  16. On the fifth day of Christmas my true love gave to me
  17. five gold rings;
  18. four calling birds, three french hens, two turtle doves
  19. and a partridge in a pear tree.
  20.  
  21. On the sixth day of Christmas my true love gave to me
  22. six geese a-laying, five gold rings;
  23. four calling birds, three french hens, two turtle doves
  24. and a partridge in a pear tree.
  25.  
  26. On the seventh day of Christmas my true love gave to me
  27. seven swans a-swimming,
  28. six geese a-laying, five gold rings;
  29. four calling birds, three french hens, two turtle doves
  30. and a partridge in a pear tree.
  31.  
  32. On the eigth day of Christmas my true love gave to me
  33. eight maids a-milking, seven swans a-swimming,
  34. six geese a-laying, five gold rings;
  35. four calling birds, three french hens, two turtle doves
  36. and a partridge in a pear tree.
  37.  
  38. On the ninth day of Christmas my true love gave to me
  39. nine ladies dancing, eight maids a-milking, seven swans a-swimming,
  40. six geese a-laying, five gold rings;
  41. four calling birds, three french hens, two turtle doves
  42. and a partridge in a pear tree.
  43.  
  44. On the tenth day of Christmas my true love gave to me
  45. ten lords a-leaping,
  46. nine ladies dancing, eight maids a-milking, seven swans a-swimming,
  47. six geese a-laying, five gold rings;
  48. four calling birds, three french hens, two turtle doves
  49. and a partridge in a pear tree.
  50.  
  51. On the eleventh day of Christmas my true love gave to me
  52. eleven pipers piping, ten lords a-leaping,
  53. nine ladies dancing, eight maids a-milking, seven swans a-swimming,
  54. six geese a-laying, five gold rings;
  55. four calling birds, three french hens, two turtle doves
  56. and a partridge in a pear tree.
  57.  
  58. On the twelfth day of Christmas my true love gave to me
  59. twelve drummers drumming, eleven pipers piping, ten lords a-leaping,
  60. nine ladies dancing, eight maids a-milking, seven swans a-swimming,
  61. six geese a-laying, five gold rings;
  62. four calling birds, three french hens, two turtle doves
  63. and a partridge in a pear tree.
Posted by Wei@13:07 December 16th, 2005 CST in Uncategorized | Permalink | Trackback.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.