C语言混乱大赛1987年获奖作品
扫描二维码
随时随地手机看文章
今天翻前两天刚送到的《C专家编程》,章节8.10的那个IOCCC 1987年的获奖作品让我觉得很好玩。
main(){ printf(&unix["/021%six/012/0"], (unix)["have"] + "fun" - 0x60);}
作者这里利用的第一个技巧并不算太晦涩:"a[i] = i[a]= *(a+i)“ 即下标运算符的可交换性。
但是这个老天爷的unix和&unix是怎么回事?
我把自己脑子里有印象的C的最隐蔽的角落回想了一遍,难道是trigraph之类的特殊符号?查了查不是
正推不行,就反推吧。编译运行后,输出结果是“unix“。如此说来“(unix)["have"] + "fun" - 0x60)“这个表达式的结果应该是指向"un"的char * ;因此,“(unix)["have"] - 0x60"的值应该是整数1才对;因此“(unix)["have"]"的值应该是0x61;这就豁然开朗了,0x61='a',是"have"中的第二个字符,所以unix的值必然为1。
这个反推是否正确呢? 利用gcc看一下只进行preprocessor处理后的代码:
whodare@whodare:~/programming/c++$ gcc -E 1.c
main()
{
printf(&1["/021%six/012/0"], (1)["have"] + "fun" - 0x60) ;
}
正确。unix看来是属于预定义的marco,值为1。拿“gcc predefined marco"为关键字google了一下,找到可信资料了:
http://gcc.gnu.org/onlinedocs/cpp/System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros
原来unix这个宏是属于gcc提供的 System-specific Predefined Macros,因此这个奇妙的函数只能在*nix环境下正常工作。
可以查看更多的gcc提供的predefined marco
gcc -dM -E 1.c
顺便附上历史文献,哈哈
Best One Liner:
David Korn
Bell Labs
MH 3C-526B, AT&T Bell Labs
Murray Hill, NJ
07974
USA
The Judges believe that this is the best one line entry ever received.
Compile on a UN*X system, or at least using a C implementation that
fakes it. Very few people are able to determine what this program
does by visual inspection. I suggest that you stop reading this
section right now and see if you are one of the few people who can.
Several points are important to understand in this program:
What is the symbol `unix' and what is its value in the program?
Clearly `unix' is not a function, and since `unix' is not declared
to be a data type (such as int, char, struct foo, enum, ...)
what must `unix' be?
What is the value of the symbol "have"? (hint: the value is
NOT 4 characters, or 'h', or a string) Consider the fact that:
char *x;
defines a pointer to a character (i.e. an address), and that
the `=' assigns things is compatible types. Since:
x = "have";
is legal C, what type of value is "have"?
Note that the following expressions yield the same value:
+ "fun" - 0x60)
*(x+3) *(3+x)
since addition is communitive. What can be said about the value:
3[x]
本文授权转载自公众号“技术让梦想更伟大”,作者李肖遥
免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!