分享:6个变态的C语言Hello World程序
本帖最后由 青蛙星 于 2015-5-31 09:29 编辑跟大家分享一下有趣的东西,记得别哭。hello world的N种写法。
第一个
#define _________ }
#define ________ putchar
#define _______ int 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>
_____
第二个
#include<stdio.h>
int main(){
int x=0,y,*z=y;*(z++)=0x48;*(z++)=y+0x1D;
*(z++)=y+0x07;*(z++)=y+0x00;*(z++)=y+0x03;
*(z++)=y-0x43;*(z++)=y-0x0C;*(z++)=y+0x57;
*(z++)=y-0x08;*(z++)=y+0x03;*(z++)=y-0x06;
*(z++)=y-0x08;*(z++)=y-0x43;*(z++)=y-0x21;
x=*(--z);while(y!=NULL)putchar(y);
}
第五个
#include<stdio.h>
int 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); }
第六个
#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; }
第三个 #include<stdio.h>
#define __(a) goto a;
#define ___(a) putchar(a);
#define _(a,b) ___(a) __(b);
int 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)}
第四个
#include <stdio.h>
int n[]={0x48,
0x65,0x6C,0x6C,
0x6F,0x2C,0x20,
0x77,0x6F,0x72,
0x6C,0x64,0x21,
0x0A,0x00},*m=n;
int main(int* n){putchar
(*m)!='\0'?main
(m++):0;}
在VS2010下实测有效。
转自:http://coolshell.cn/articles/914.html
-----------------------------------------------------------------------------------------------------------------------------------------------------
一位网友的讲解
第一个:利用了宏的层层替代,剥离开来就是一个标准的 main(){putchar(‘H’);……putchar(‘!’);}
第二个:利用数组和指针,先对赋值Y=0x48=’H’,Y=Y+0x1D=0x65=’e’依次通过偏移量对数组赋值,组后逐个输出。
第三个:利用goto语言跳转输出。从 __(t)开始,即goto t;t:_(‘H’,j) 即(putchar(‘H’, goto (j)) 依次跳转输出。
第四个:利用函数副作用和条件运算符判断输出。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'.
有点意思:lol: 因复仇而活 发表于 2015-5-31 04:46
有点意思
已更新,之前的因为插入代码功能不完善,导致排版并不是我想要的结果。这次第三,第四的代码已更新。 实验成功,虽然不明白怎么做到的。 对于初学者的我来说,表示完全看不懂 好霸气的写法。 @Angel丶L @s0512 @kklloo @wei_Y 强烈支持楼主ing…… 真是够变态的写法啊~ 支持下 乱码大赛里面很多这种 太厉害了哈 还真的是,条条大路捅罗马,脑洞打开 世界的恶意= = 简直是牛逼轰轰 必须支持! 路过飘过 这还是我第一次看啊 确实想哭。。。。
对于初学者的我来说,表示完全看不懂