为什么C语言还这么流行?

编程与设计 TOMORROW 来源:码农翻身 8个月前 (10-15) 1014次浏览 31个评论 扫描二维码
评价任何一门编程语言,都是招人骂的。 永远是这样。就像是春寒料峭的季节, 街上穿棉袄和穿单衣的擦肩而过,双方一定是同时在心里出现了两个字:“傻逼!”这个在心理学上有个专业的名字:叫做“二逼”现象! 那我为啥还要做这个挨骂的事呢?作为《C 语言点滴》《drop of knowledge of C++》书籍的作者,《C 语言新思维,第二版》的译者。我觉得我有责任系统的介绍一下这本语言,他的特点,还有他的未来。这个问题对很多刚刚踏入程序猿这个行业的新手至关重要。因为他们有深深的担忧,万一C 语言就像 Fortran,perl 语言那样过时了怎么办? 

先上一个表,这个就是著名的 TIOBE 语言排行榜。目前它是一个最权威的一个语言流行度的排行榜,从这个排行榜上看,你会得到一个最直观的结论。Java 和 C 都在下降,而下降的部分被第三名以后的语言所瓜分。 

为什么 C 语言还这么流行?

(码农翻身注:这是去年的排行榜,最新的 TIOBE 数据显示 Java 和 C 分别以 17.8%和 15.37%排在前两名)

对所有的编程语言,他们的最后的目的其实就是两种:提高硬件的运行效率和提高程序员的开发效率。

遗憾的是,这两点是不可能并存的!你只能选一样。在提高硬件的运行效率这一方面,C 语言没有竞争者!举个简单的例子,实现一个列表,C 语言用数组 int a[3],经过编译以后变成了(基地址+偏移量)的方式。对于计算机来说,没有运算比加法更快,没有任何一种方法比(基地址+偏移量)的存取方法更快。

C 语言已经把硬件的运行效率压缩到了极致。这种设计思想带来的问题就是易用性和安全性的缺失。例如,你不能在数组中混合保存不同的类型,否则编译器没有办法计算正确的偏移量。同时 C 语言对于错误的偏移量也不闻不问,这就是 C 语言中臭名昭著的越界问题。

C 语言自诩的“相信程序员”都是漂亮的说辞,它的唯一目的就是快,要么飞速的运行,要么飞速的崩溃。C 语言只关心程序飞的高不高,不关心程序猿飞的累不累。就是这样! 

现在来看看那些非 C 的语言,他们的长处都在于提高程序员的开发效率上。或者支持动态的列表,或者支持安全的列表。但是加入任何的中间层,加入任何的安全检验,它不可能比(基地址+偏移量+无检验)的方式更快。这个世界上不存在“开发容易,运行快”的语言,开发容易毕竟来源于对底层的一层一层又一层的包装。 

现在回答两个最普遍的问题:硬件这么便宜了,有必要让软件更快吗?有这种疑问的人大部分都是网吧的固定客户,他们理解的计算机只在电脑城,他们理解的计算只是游戏和播放硬盘中的小电影。不要玩个游戏开个挂就乐得不行不行的,别忘了还有全实景仿真,还有 3D 渲染,还有自动驾驶。

人在开车的时候,每秒要收集 60 个不同的物体,然后根据这 60 个物体的不同组合和反映来做 20 个最重要的决定。然后从这 20 多个决定中选一个执行。所以就算用上最快的硬件,自动驾驶现在还不敢说能像人那样开车。就算是自动驾驶成功了,下一步还要自动飞行呢?因为我们老早就预言了:你咋不上天呢!

所以说:计算速度永远是不够的!因为新的应用会越来越复杂,越来也实时。对了!我还忘了一个更重要的限制:计算的能耗!NASA 飞行器上的 CPU 最多就是 32 位的,说出来你可能不信,国际空间站上没有一个 CPU 是 64 位的,我猜一个最主要的原因是航天员不爱看硬盘小电影吧。 

另外一个流行的疑问是:我可以发明一种同样快的语言,但是没有 C 语言那么多的坑。想法是可以的,而且还真巧有这个语言,真巧它的名字叫 D 语言,真巧没有太多的人用!这是因为一个基本的事实。现在有太多,太多太多的 C 代码,他们大部分都在正常工作,就像 Linux, Window, MacOS,Unix,Vxworks。你没有看错,这些操作系统的内核都是 C,我虽然不确定 C 在 Window 中所占的比例,但是我相信微软的人不会傻到用 C#去全部改写一个操作系统的内核。你想让这些人去用你的全新的语言,这就不是“有点”很傻,很天真了!

而且有些代码,我们根本就不能改!NASA 一个简单的 5 个 CPU 飞控软件编写完毕后,要进行一种“全覆盖”测试。如果 CPU A 坏了会发生什么?如果 CPU A,B 坏了呢?如果 CPU A,C 坏了呢。。。。?如果你愿意,你可以做个简单的数学组合。测试完毕后,别说重写,就算加个注释都不行。因为主管 payload 的大妈会非常严肃的质问你,为什么你上报的东西数量增加了,但是质量没有增加?你需要和她详细的解释:硬件和软件是不同的,硬件是那种摸起来硬硬的东西,但是软件不是那种摸起来软软的东西。看着大妈鄙夷的眼神,这个时候你会非常后悔自己手欠加入的哪一行注释。你还别不当真,这个是 NASA 的真实故事。 

哪为什么 C 语言还下降这么多呢?很简单,有些任务本身就不是 C 语言的。我上学的时候还用 C 语言编过窗口界面呢?然后很快微软的人就推出了 MFC,就是一大堆宏把底层的 C windowAPI 包装了起来。

再后来这个技术也过时了。因为微软的人认识到,带有窗口的应用程序说到底不是 C 语言的本职工作,再这么一层一层包下去就有露馅的危险,于是他们发明了一个全新的语言 C#来负责这个任务。

Java 也是这样,突出网络,易用,安全,跨平台。无论是 Java, c#还是 python, 他们都有意避开提高硬件的运行效率这个问题,因为这个问题上没办法和 C 竞争,也无法撼动 Linux, Unix,GNU tool 这些已有 C 代码的位置。剩下的就只是提高程序员的开发效率上大作文章。这对 C 语言是好事,把自己不善长的东西去掉,让自己跑的更快! 

伴随着嵌入和实时系统的兴起,AI,机器人,自动驾驶等。这些都是 C 语言的核心应用,而且在这种应用上面,C 语言没有竞争者。所以我感觉 C 语言会稳定在自己核心的应用中,并开始逐步回升。(码农翻身注:赵老师预料对了,果然回升了。)

但是 Java 语言我个人不乐观。小型和灵活性上,Python 更胜一筹。一行 python 代码后,你根本不知道自己还是不是 duck 类型?平台领域,每个平台都推出自己专属的语言。Windows 会继续支持 C#,苹果偏爱 Swift, Android 推出 Kotlin,Google 用 go。Java 宣称自己可以自由到每家做客,但是无论是到谁家,都会发现客厅里面坐着一个亲儿子,这个时候自己这个干儿子多多少少有点尴尬。所以我猜测,最后 Java 会稳定在对跨平台有严格要求的,大型非实时应用上。 

最后说点闲话,C++不会淘汰 C 语言。有了对象后你会发现再简朴的对象也耗费资源,而且有了对象以后,总是不由自主的去想继承这个事,一但继承实现了,你会发现继承带来的麻烦远超过你的想象。Java 的发明人 James 被问到如果可以从新设计 Java 语言的话,第一个要做什么事?他说:“去掉对象”!作为一个已婚,有两个孩子的程序猿,我感同身受。如果大家感兴趣,我可以再写一个博客,聊聊 C++和 C 的真实区别所在。  

如果你看到这里,还什么都没记住。那就只记住一点:没人能预测未来。 

全世界只需要五台电脑 -IBM 创始人 

640K 内存足够了 -微软创始人 

没必要在家里用电脑-DEC 创始人 

如果再有人对你说 C 语言已经过时了,最好自己思考一下,能求真最好,如果不能,至少要做到存疑。  


TOMORROW 星辰 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:为什么 C 语言还这么流行?
喜欢 (6)
TOMORROW
关于作者:
一个从石头坑掉到泥坑里的攻城狮。
激动的蜡烛发表我的评论  请填写正确的 个人信息
取消评论
表情 加粗 斜体 签到
(31)个小伙伴在吐槽
  1. I must express some thanks to this writer just for bailing me out of this type of trouble. As a result of checking throughout the the web and meeting notions which are not pleasant, I figured my entire life was over. Existing devoid of the answers to the problems you have resolved by way of your good website is a serious case, as well as ones which might have adversely affected my career if I hadn't discovered your web site. The capability and kindness in handling all the pieces was useful. I am not sure what I would have done if I hadn't come upon such a thing like this. I can at this point look ahead to my future. Thank you very much for the high quality and result oriented help. I won't be reluctant to suggest your site to anyone who should have assistance on this subject matter.
    yeezy boost 350 v22019-06-07 05:00 回复 Windows 7 | 未知浏览器
  2. I simply needed to thank you very much once more. I do not know what I could possibly have carried out without the type of techniques shown by you directly on such a subject. It was actually the difficult issue for me, nevertheless noticing this specialised strategy you resolved the issue took me to cry over contentment. I am just happier for this work as well as believe you are aware of a great job you are accomplishing training others with the aid of your site. Probably you've never met any of us.
    yeezy boost2019-06-05 08:23 回复 Windows 7 | 未知浏览器
  3. I intended to send you that bit of note in order to give many thanks over again with the nice solutions you have documented above. It is really strangely generous of you to deliver unreservedly just what a lot of people would've supplied for an electronic book to help with making some cash for themselves, especially since you might well have tried it in case you decided. Those ideas additionally worked as the great way to be certain that other people online have similar fervor similar to my very own to find out much more related to this problem. I believe there are a lot more pleasurable opportunities in the future for individuals that look over your website.
    michael kors outlet2019-06-01 07:57 回复 Windows 7 | 未知浏览器
  4. I precisely wished to say thanks once again. I do not know the things I would've done in the absence of these creative concepts discussed by you over such subject. Previously it was the terrifying situation in my opinion, nevertheless noticing the skilled approach you dealt with the issue made me to cry over happiness. I'm just grateful for the guidance as well as sincerely hope you are aware of a powerful job you happen to be undertaking teaching other individuals using your blog. I am sure you haven't encountered any of us.
    jordan shoes2019-05-28 07:18 回复 Windows 7 | 未知浏览器
  5. I simply wanted to make a simple remark so as to say thanks to you for those unique recommendations you are posting at this website. My time intensive internet lookup has at the end been rewarded with reasonable details to go over with my pals. I 'd declare that many of us site visitors actually are very endowed to be in a fabulous website with many brilliant professionals with great opinions. I feel very grateful to have come across your entire website and look forward to really more enjoyable moments reading here. Thanks a lot once more for everything.
    yeezy shoes2019-05-26 09:11 回复 Windows 7 | 未知浏览器
  6. Needed to post you a little bit of note in order to thank you so much once again considering the extraordinary techniques you've shared on this website. This is quite strangely open-handed with you to make easily precisely what a number of us would have advertised as an ebook to help make some profit for themselves, specifically seeing that you might well have done it in case you wanted. These principles additionally worked like the great way to realize that someone else have a similar desire like mine to understand many more in terms of this problem. I am sure there are numerous more enjoyable times up front for folks who take a look at your website.
    curry 52019-05-24 06:59 回复 Windows 7 | 未知浏览器
  7. Thanks a lot for providing individuals with an extraordinarily spectacular opportunity to read articles and blog posts from this website. It's always very kind and also packed with amusement for me personally and my office friends to visit the blog nearly thrice a week to find out the new things you have. Of course, I'm usually motivated with the powerful secrets you give. Some 2 facts in this article are undoubtedly the finest we have all had.
    vapormax2019-05-20 05:19 回复 Windows 7 | 未知浏览器
1 2