C语言的爱,你永不悔!代码的关怀,你受不起!

109 阅读2分钟

#前言

今天在朋友圈看到的一个小代码,我觉得特别有意思,然后我就转发了下,好多好多同学在下面留言。其中也有讨论了技术的问题,所以我就总结下发出来。

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);}
![](https://upload-images.jianshu.io/upload_images/24175598-ca5280e5ec3b7eeb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#这代码会不会导致电脑死机

电脑有操作系统,肯定不会死机,但是因为while里面没有sleep,操作系统在调度的时候,程序一直占用cpu,会导致cpu占用率比较高。

如果是单片机的话,就只有一个程序,这样的话,其他程序就用不了了。

![](https://upload-images.jianshu.io/upload_images/24175598-427cbeca59dca72a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#如果一直申请内存呢?会不会导致电脑挂机?

理论分析下,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);}

这个会死机的哦~

![](https://upload-images.jianshu.io/upload_images/24175598-44710042b27f2f73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#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编程、游戏编程、黑客等等......

![](https://upload-images.jianshu.io/upload_images/24175598-adb024eec012218f.gif?imageMogr2/auto-orient/strip)

编程入门资料:

![](https://upload-images.jianshu.io/upload_images/24175598-8bf19ae804429630.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

​推荐学习书籍:

![](https://upload-images.jianshu.io/upload_images/24175598-0cbb1bb1ed7664ba.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

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