C语言混乱大赛1987年获奖作品
2008-02-20 21:57
323 查看
今天翻前两天刚送到的《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
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 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:
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]
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
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 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:
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]
相关文章推荐
- 国际混乱C语言大赛获奖作品解析示例
- 国际混乱C语言大赛获奖作品解析示例(转载)
- 国际C语言混乱大赛获奖代码一条
- 第二届中国元素国际创意大赛获奖作品赏
- 美不胜收 首届CSS世界大赛获奖作品欣赏
- ida 2016插件大赛获奖作品
- 2006CSS世界大赛获奖作品源代码学习(一)
- CocoaChina2012年粒子特效大赛获奖作品源码分享
- 2006CSS世界大赛获奖作品源代码学习(二)
- [转贴]Excel操作技巧大全(微软Office技巧大赛获奖作品)
- Android大赛首轮获奖作品解析。。。
- 1987年C语言乱码大赛之最优秀单行代码
- 贴几张去年指导的全国高校学生GIS应用开发大赛获奖作品(一等奖)界面
- Android大赛首轮获奖作品解析。。。
- 国际混淆C代码大赛获奖作品部分源码
- 黄河金岸诗词大赛获奖作品选登(1)
- 第二次Google Android开发大赛的获奖作品,有的的确很有创意。
- 国际C 语言乱码大赛(IOCCC)获奖作品
- Android大赛首轮获奖作品解析
- 国际C 语言乱码大赛(IOCCC)1988年获奖作品