本文主要是介绍【C语言笔记】【有点逗系列】 一个打印 Hello, world! 的程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【C语言笔记】【有点逗系列】 一个打印 Hello, world! 的程序
有点逗系列内容。用于记录各式各样有点逗甚至没有什么用的C语言用法☺。
功能说明
在网上看到了一个表面上很混乱但是实则没什么用处的打印 Hello, world! 的程序。
程序如下:
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]); }
这个程序表面上完全不知道在做什么,但实际上就是利用位运算和数组,将字符串 “Hello, World!” 压缩成了一系列整数,最后再打印出来。
将代码编译运行后的结果如下:
Hello, world!
我们来把上面的代码重新格式化过后,再加上详细注释,变成如下代码:
#include <stdio.h>int main() {int i;int n[] = {(((1<<1)<<(1<<1)<<(1<<1)<<(1<<(1>>1)))+((1<<1)<<(1<<1))), // H(((1<<1)<<(1<<1)<<(1<<1)<<(1<<1))-((1<<1)<<(1<<1)<<(1<<1))+((1<<1)<<(1<<(1>>1)))+(1<<(1>>1))), // e(((1<<1)<<(1<<1)<<(1<<1)<<(1<<1))-((1<<1)<<(1<<1)<<(1<<(1>>1)))-((1<<1)<<(1<<(1>>1)))), // l(((1<<1)<<(1<<1)<<(1<<1)<<(1<<1))-((1<<1)<<(1<<1)<<(1<<(1>>1)))-((1<<1)<<(1<<(1>>1)))), // l(((1<<1)<<(1<<1)<<(1<<1)<<(1<<1))-((1<<1)<<(1<<1)<<(1<<(1>>1)))-(1<<(1>>1))), // o(((1<<1)<<(1<<1)<<(1<<1))+((1<<1)<<(1<<1)<<(1<<(1>>1)))-((1<<1)<<(1<<(1>>1)))), // ,((1<<1)<<(1<<1)<<(1<<1)), // (((1<<1)<<(1<<1)<<(1<<1)<<(1<<1))-((1<<1)<<(1<<1))-(1<<(1>>1))), // w(((1<< 1)<<(1<<1)<<(1<<1)<<(1<<1))-((1<<1)<<(1<<1)<<(1<<(1>>1)))-(1<<(1>>1))), // o(((1<<1)<<(1<<1)<<(1<<1)<<(1<<1))- ((1<<1)<<(1<<1)<<(1<<(1>>1)))+(1<<1)), // r(((1<<1)<<(1<<1)<<(1<<1)<< (1<<1))-((1<<1)<<(1<<1) <<(1<<(1>>1)))-((1<<1)<<(1<< (1>>1)))), // l(((1<<1)<<(1<<1)<<(1<<1)<< (1<<1))- ((1<<1)<<(1<<1)<<(1<<1))+((1<<1)<< (1<<(1>>1)))), // d(((1<<1)<<(1<<1) <<(1<<1))+(1<<(1>>1))), // ! (((1<<1)<<(1<<1))+((1<<1)<<(1<<(1>>1))) + (1<<(1>>1))) // \r}; for (i = (1 >> 1); i < (((1 << 1) << (1 << 1)) + ((1 << 1) << (1 << (1 >> 1))) + (1 << 1)); i++) {printf("%c",n[i]);}return 0;
}
现在再看代码就清晰很多了,可以看出是要打印 “Hello, world!” 了。
这代码的可读性非常差,罗列紧凑的位运算难以理解,使用了不寻常的写法,利用了位运算来表示字符的ASCII码,将字符串 “Hello, World!” 压缩成了一系列整数。如果要看懂需要对C语言位运算有一定的熟练度。这代码在技术上是一个有趣的练习,可以展示对位运算和ASCII码的理解。
【参考资料】
6个变态的C语言HELLO WORLD程序
本文链接:https://blog.csdn.net/u012028275/article/details/137424102
这篇关于【C语言笔记】【有点逗系列】 一个打印 Hello, world! 的程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!