6个变态的C语言Hello World程序

6个变态的C语言Hello World程序

下面的六个程序片段主要完成这些事情:

  1. 输出Hello, World
  2. 混乱C语言的源代码

下面的所有程序都可以在GCC下编译通过,只有最后一个需要动用C++的编译器g++才能编程通过。

hello1.c

    #define _________ }
    #define ________ putchar
    #define _______ main
    #define _(a) ________(a);
    #define ______ _______(){
    #define __ ______ _(0x48)_(0x65)_(0x6C)_(0x6C)
    #define ___ _(0x6F)_(0x2C)_(0x20)_(0x77)_(0x6F)
    #define ____ _(0x72)_(0x6C)_(0x64)_(0x21)
    #define _____ __ ___ ____ _________
    #include<stdio.h>
    _____

hello2.c 

    #include<stdio.h>
    main(){
      int x=0,y[14],*z=&y;*(z++)=0x48;*(z++)=y[x++]+0x1D;
      *(z++)=y[x++]+0x07;*(z++)=y[x++]+0x00;*(z++)=y[x++]+0x03;
      *(z++)=y[x++]-0x43;*(z++)=y[x++]-0x0C;*(z++)=y[x++]+0x57;
      *(z++)=y[x++]-0x08;*(z++)=y[x++]+0x03;*(z++)=y[x++]-0x06;
      *(z++)=y[x++]-0x08;*(z++)=y[x++]-0x43;*(z++)=y[x]-0x21;
      x=*(--z);while(y[x]!=NULL)putchar(y[x++]);
    }

hello3.c

    #include<stdio.h>
    #define __(a) goto a;
    #define ___(a) putchar(a);
    #define _(a,b) ___(a) __(b);
    main()
    { _:__(t)a:_('r',g)b:_('$',p)
      c:_('l',f)d:_(' ',s)e:_('a',s)
      f:_('o',q)g:_('l',h)h:_('d',n)
      i:_('e',w)j:_('e',x)k:_('\n',z)
      l:_('H',l)m:_('X',i)n:_('!',k)
      o:_('z',q)p:_('q',b)q:_(',',d)
      r:_('i',l)s:_('w',v)t:_('H',j)
      u:_('a',a)v:_('o',a)w:_(')',k)
      x:_('l',c)y:_('\t',g)z:___(0x0)}

hello4.c

    int n[]={0x48,
    0x65,0x6C,0x6C,
    0x6F,0x2C,0x20,
    0x77,0x6F,0x72,
    0x6C,0x64,0x21,
    0x0A,0x00},*m=n;
    main(n){putchar
    (*m)!='\0'?main
    (m++):exit(n++);}

hello5.c

    main(){int i,n[]={(((1<<1)<<(1<<1)<<(1<<
    1)<<(1<<(1>>1)))+((1<<1)<<(1<<1))), (((1
    <<1)<<(1<<1)<<(1<<1)<<(1<<1))-((1<<1)<<(
    1<<1)<<(1<<1))+((1<<1)<<(1<<(1>>1)))+ (1
    <<(1>>1))),(((1<<1)<<(1<<1)<<(1<<1)<< (1
    <<1))-((1<<1)<<(1<<1)<<(1<<(1>>1)))- ((1
    <<1)<<(1<<(1>>1)))),(((1<<1)<<(1<<1)<<(1
    <<1)<<(1<<1))-((1<<1)<<(1<<1)<<(1<<(1>>1
    )))-((1<<1)<<(1<<(1>>1)))),(((1<<1)<< (1
    <<1)<<(1<<1)<<(1<<1))-((1<<1)<<(1<<1)<<(
    1<<(1>>1)))-(1<<(1>>1))),(((1<<1)<<(1<<1
    )<<(1<<1))+((1<<1)<<(1<<1)<<(1<<(1>>1)))
    -((1<<1)<<(1<<(1>>1)))),((1<<1)<< (1<<1)
    <<(1<<1)),(((1<<1)<<(1<<1)<<(1<<1)<<(1<<
    1))-((1<<1)<<(1<<1))-(1<<(1>>1))),(((1<<
    1)<<(1<<1)<<(1<<1)<<(1<<1))-((1<<1)<< (1
    <<1)<<(1<<(1>>1)))-(1<<(1>>1))), (((1<<1
    )<<(1<<1)<<(1<<1)<<(1<<1))- ((1<<1)<< (1
    <<1)<<(1<<(1>>1)))+(1<<1)), (((1<<1)<< (
    1<<1)<<(1<<1)<< (1<<1))-((1<<1)<< (1<<1)
    <<(1<<(1>>1)))-((1<<1) <<(1<< (1>>1)))),
    (((1<<1)<< (1<<1)<<(1<<1)<< (1<<1))- ((1
    <<1)<<(1<<1)<<(1<<1))+((1<<1)<< (1<<(1>>
    1)))), (((1<<1)<<(1<<1) <<(1<<1))+(1<<(1
    >>1))),(((1<<1)<<(1<<1))+((1<<1)<< (1<<(
    1>>1))) + (1<< (1>>1)))}; for(i=(1>>1);i
    <(((1<<1) <<(1<<1))+((1 <<1)<< (1<<(1>>1
    ))) + (1<<1)); i++) printf("%c",n[i]); }

hello6.cpp

下面的程序只能由C++的编译器编译(比如:g++)

    #include <stdio.h>
    #define _(_) putchar(_);
    int main(void){int i = 0;_(
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++i)_(++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++++++++i)_(++++++++++++++
    i)_(--++i)_(++++++i)_(------
    ----------------------------
    ----------------------------
    ----------------------------
    ----------------------------
    ----------------i)_(--------
    ----------------i)_(++++++++
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++i)
    _(----------------i)_(++++++
    i)_(------------i)_(--------
    --------i)_(----------------
    ----------------------------
    ----------------------------
    ----------------------------
    ----------------------------
    ------i)_(------------------
    ----------------------------
    i)return i;}

(全文完)

(转载本站文章请注明作者和出处 酷 壳 – CoolShell ,请勿用于任何商业用途)

好烂啊有点差凑合看看还不错很精彩 (152 人打了分,平均分: 4.63 )
Loading...

6个变态的C语言Hello World程序》的相关评论

  1. 用这个命令展开宏很方便看出宏实际效果
    gcc -E -P
    比如第一个
    结果为
    main(){ putchar(0x48);putchar(0x65);putchar(0x6C);putchar(0x6C); putchar(0x6F);putchar(0x2C);putchar(0x20);putchar(0x77);putchar(0x6F); putchar(0x72);putcha
    r(0x6C);putchar(0x64);putchar(0x21); }
    (去掉#include这一行,不然有很多stdio.h中信息)

  2. 为何俺的 gcc 在第5个程序上编译出来东西呢?
    只有一个warning,生成了一个 a.out 但是运行之后啥也不输出
    感谢前排的提示 -E -P 很好用的说~

  3. hello2.c

    在VS下是编译不通过的。 int *z = &y ;
    应该是 y 吧?90后斗胆质疑了一下

  4. 第一个:利用了宏的层层替代,剥离开来就是一个标准的 main(){putchar(‘H’);……putchar(‘!’);}
    第二个:利用数组和指针,先对赋值Y[0]=0x48=’H’,Y[1]=Y[0]+0x1D=0x65=’e’依次通过偏移量对数组赋值,组后逐个输出。
    第三个:利用goto语言跳转输出。从 __(t)开始,即goto t;t:_(‘H’,j) 即(putchar(‘H’, goto (j)) 依次跳转输出。
    第四个:第一次见main函数可以这样应用,利用函数副作用和条件运算符判断输出。n是数组首地址。m是指针变量,每次自增,最后函数返回数组首地址。
    第五个:利用移位(结核性:L-R)对数组赋值:如第一个值((1<<1)<<(1<<1)<<(1<<1)<<(1<>1)))+((1<<1)<<(1<<1))),即((2<<2<<2<<1) + (2<<2))即(8<<2<<1 + 8)= 64<<1 + 8 = 64 + 8 = 72 = 0x48 ='H'.
    第六个,cpp不熟不懂。
    关于第五个没有输出的问题:其实通过调试可以发现其实是输出了的,只是数值组后一个元素n[13] = 0x0D= '\r',回车,将当前位置移到本行头
    更改下n[13] = 0x0A = '\n' = ((1<<1)<<(1<<1)+(1<<1)), 就可以看到输出了。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注