• IOCCC 1987 最佳单行代码解读


    /* ioccc.c */
    
    /* IOCCC best one-liner winner 1987 by David Korn ---
    
    main() { printf(&unix["21%six12"],(unix)["have"]+"fun"-0x60);}
    
    from <http://www.ioccc.org/years.html#1987>
    */
    
    /* A detailed set of samples to show how this works
       by David Ireland, copyright (C) 2002.
    
       Modified by William Cheung
         for GCC Version 4.8.3 on CentOS 7 (x86_64)
       See 
           http://www.di-mgt.com.au/src/korn_ioccc.txt
         for original code
    */
    
    #include <stdio.h>
    
    int main() 
    {
        
        // int unix;
        // We do not need to declare 'unix', or we will get an error:
        //   expected identifier or ‘(’ before numeric constant
        // because unix is a predefined macro that expands to 1 (only on 
        // unix-like systems perhaps) 
    
        printf("unix=%d
    ", unix); /* =1 */    
    
        /* This prints the string "un", 
           i.e. "fun" starting at offset [1] */
        printf("%s
    ","fun"+1);
    
        /* This prints 97 = the int value of the 2nd char 'a' */
        printf("%d
    ", "have"[1]);
    
        /* just like this */
        printf("%d
    ", 'a');
    
        /* ditto because x[1] = 1[x] */
        printf("%d
    ", (1)["have"]);
    
        /* 97 - 96 = 0x61 - 0x60 = 1 */
        printf("%d
    ", (1)["have"] - 0x60);
    
        /* So this is the same as "fun" + 1, printing "un" */
        printf("%s
    ", "fun" + ((1)["have"] - 0x60));
    
        /* Rearrange and use unix variable instead of 1 */
        printf("%s
    ", (unix)["have"]+"fun"-0x60);
    
        /* ...thus we have the first argument in the printf call. */
    
        /* Both these print the string "bcde", ignoring the 'a' */
        printf("%s
    ", "abcde" + 1);
        printf("%s
    ", &"abcde"[1]);
    
        /* so does this */
        printf("%s
    ", &(1)["abcde"]);
    
        /* and so does this (NB [] binds closer than &) */
        printf("%s
    ", &unix["abcde"]);
    
        /* This prints "%six" + newline */
        printf("%s", &"?%six
    "[1]);
    
        /* So does this: note that
           12 = 0x0a = 
    ,
           the first 21 char is ignored,
           and the  is superfluous, probably just for symmetry */
        printf("%s", &"21%six12"[1]);
    
        /* and so does this */
        printf("%s", &unix["21%six12"]);
    
        /* Using this as a format string, we can print "ABix" */
        printf(&unix["21%six12"], "AB");
    
        /* just like this does */
        printf("%six
    ", "AB");
    
        /* So, we can print "unix" like this */
        printf("%six
    ", (unix)["have"]+"fun"-0x60);
        
        /* or, finally, like this */
        printf(&unix["21%six12"],(unix)["have"]+"fun"-0x60);
    
        return 0;
    }
  • 相关阅读:
    SQL Server 限制IP登陆
    提高MSSQL数据库读取速度,降低CPU损耗
    Windows Azure 平台开发基础系列视频
    AutoFac使用方法总结:Part III
    Python学习笔记 01 快速入门
    黑苹果~~
    Python学习笔记 02 Python基础
    Python学习笔记 04 数字
    Python学习笔记 03 Python对象
    Gridview导出excel范例
  • 原文地址:https://www.cnblogs.com/william-cheung/p/5371110.html
Copyright © 2020-2023  润新知