当前位置:首页 > > 嵌入式微处理器
[导读]今天翻前两天刚送到的《C专家编程》,章节8.10的那个IOCCC 1987年的获奖作品让我觉得很好玩。 main(){ printf(&unix["/021%six/012/0"], (unix)["have"] + "fun" - 0x60);} 作者这里利用的第一个技巧并不算太晦涩:"a[i] = i[a]=  *(a+i)“ 即下标运算符的可

今天翻前两天刚送到的《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.cmain(){ 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 AT&T 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 thatfakes it. Very few people are able to determine what this programdoes by visual inspection. I suggest that you stop reading thissection right now and see if you are one of the few people who can.

Several points are important to understand in this program:

1) 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?

2) 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"?

3) Note that the following expressions yield the same value:(unix)["have"] + "fun" - 0x60) x[3] *(x+3) *(3+x)

since addition is communitive. What can be said about the value:

3[x]

本文授权转载自公众号“技术让梦想更伟大”,作者李肖遥

免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!

嵌入式ARM

扫描二维码,关注更多精彩内容

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭