TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Who Says C is Simple? (2010)

117 点作者 StylifyYourBlog大约 10 年前

8 条评论

copsarebastards大约 10 年前
All of the examples here are <i>really</i> horrible code. This is the second article in a few days on Hacker News to list out a few examples of how hard C is. And for little reason; there&#x27;s <i>absolutely no value</i> in being able to write something like:<p><pre><code> return ({goto L; 0;}) &amp;&amp; ({L: 5;}); </code></pre> It probably has a bug, will be hard to debug, and isn&#x27;t more performant than writing it in a clearer way. And unfortunately, while the examples here are probably all contrived, there are plenty of real-life cases where code as bad as this gets into production systems.<p>So why are we still writing code like this?<p>The answer is reverse compatibility. Not just of compilers, but of tools and skillsets: people are unwilling to support multiple versions of C and want their code to run forever.<p>Objective-C and C++ do things to add functionality to C, but they don&#x27;t remove the functionality of C that allows these kinds of problems.<p>This points to a need for a new language that avoids these issues. I think Rust is the answer, but I would like to see more languages try to fill that gap--competition is healthy.
评论 #9170024 未加载
评论 #9169887 未加载
评论 #9169767 未加载
评论 #9170277 未加载
cbd1984大约 10 年前
C is simple to compile into nearly-optimal code, or at least it was back in the 1970s, when computers had single-opcode dispatch or trivial pipelines, no SIMD hardware, no other parallelism worth mentioning, and it wasn&#x27;t worth worrying about cache too much. (Running in the registers was a neater trick.)<p>That meant it was relatively simple to &#x27;see&#x27; the assembly language &#x27;behind&#x27; a given C function or stretch of code; it didn&#x27;t take much to get inside the head of a C compiler, so you could be reasonably sure that a simple piece of C would result in a similarly simple piece of assembly out the other end.<p>That, of course, was well and good when it was reasonably simple to predict actual performance from glancing at assembly code, which assumes opcode performance (as opposed to, say, cache performance) dominates how fast the code runs.<p>Now... how many of those things still hold true on desktop and server class hardware?
评论 #9168230 未加载
评论 #9168697 未加载
评论 #9168744 未加载
Moral_大约 10 年前
The first two invoke UB and are thus completely illogical.
评论 #9167666 未加载
评论 #9167604 未加载
评论 #9168123 未加载
评论 #9167510 未加载
评论 #9167591 未加载
kazinator大约 10 年前
I stopped reading after the explanations about <i>return ((1 - sizeof(int)) &gt;&gt; 32);</i><p>Unless size_t is wider than 32 bits, it has undefined behavior. That&#x27;s why it returns 0; it could as well be 42, or the program could terminate with or without a diagnostic message, etc.
taeric大约 10 年前
Who says simple things always yield simple results?
评论 #9167816 未加载
agounaris大约 10 年前
who said C is simple? :S
评论 #9167688 未加载
评论 #9168068 未加载
评论 #9168690 未加载
评论 #9167568 未加载
评论 #9168843 未加载
dang大约 10 年前
Can anybody figure out the year on this one? Internet Archive says 2011 but it seems it might be earlier.
评论 #9167248 未加载
评论 #9167259 未加载
iopq大约 10 年前
I&#x27;ve been looking for this for months, I wanted to link this to my friend who said he prefers C to Java for his CS classes.
评论 #9169387 未加载