#前言
今天在朋友圈看到的一个小代码,我觉得特别有意思,然后我就转发了下,好多好多同学在下面留言。其中也有讨论了技术的问题,所以我就总结下发出来。
PS:文章末尾有惊喜哦!
#代码,爱你永不悔
#include <stdio.h>#define uint8 unsigned charint main(){uint8 love=0,day=1;while(love<= 365){love += day;}return (0);}
这个代码很有意思,如果你运行了,是永远达不到return的位置的,我在朋友圈写的含义是,爱你直到电脑死机,然后有一个同学留言说,应该是爱你永不返回「反悔」,这个解释特别贴切。
#代码,我的爱不能清零
#include <stdio.h>#define uint long long unsignedint main(){uint love=0,day=1;while(love >= 0){love += day;}return (0);}
我们看看long long unsigned 大小
#include <stdio.h>#include <math.h>#define uint long long unsignedint main(){uint love=0,day=1;printf("%d %f\n",sizeof(uint),pow(2,sizeof(uint)*8) -1);return (0);}

#这代码会不会导致电脑死机
电脑有操作系统,肯定不会死机,但是因为while里面没有sleep,操作系统在调度的时候,程序一直占用cpu,会导致cpu占用率比较高。
如果是单片机的话,就只有一个程序,这样的话,其他程序就用不了了。

#如果一直申请内存呢?会不会导致电脑挂机?
理论分析下,Android下都会由Lowmomerykiller,电脑也应该会有吧?所以你觉得应该不会死机?我写了两个小程序。
#include <stdio.h>#include <math.h>#include <string.h>#define uint long long unsignedint main(){uint love=0,day=1;printf("%d %f\n",sizeof(uint),pow(2,sizeof(uint)*8) -1);while(love >= 0){love += day;malloc(love);}return (0);}
这个会死机的哦~

#include <stdio.h>#include <math.h>#include <string.h>#define uint long long unsignedint main(){uint love=0,day=1;printf("%d %f\n",sizeof(uint),pow(2,sizeof(uint)*8) -1);while(love >= 0){love += day;malloc(day);}return (0);}
这个等比较久,因为申请的比较小,操作系统还能反应,所以我等了很久都没事。大家可以自己试试也可以评论说出自己的看法
————————————
看到这里你是不是对“C语言”又有了一点新的认知呢~
如果你想当学C语言,小编推荐一个程序员编程学习基地【点击进入】!
**涉及到:**C语言、C++、windows编程、网络编程、QT界面开发、Linux编程、游戏编程、黑客等等......

编程入门资料:

推荐学习书籍:

一个活跃、高逼格、高层次的编程学习殿堂;编程入门只是顺带,思维的提高才有价值!