C语言混乱大赛1987年获奖作品

本文剖析了一个复杂的C语言代码示例,该示例巧妙地利用了预定义宏和字符串操作,展示了如何通过逆向思维理解其背后的逻辑。文章还介绍了如何使用GCC预处理器来揭示代码的秘密。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天翻前两天刚送到的《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 intcharstruct 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]


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值