TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

I Do Not Know C: Short quiz on undefined behavior (2015)

214 pointsby waynecolvinover 8 years ago

19 comments

aidanhsover 8 years ago
My &#x27;favourite&#x27; bit of surprising (not undefined) behaviour I&#x27;ve seen recently in the C11 spec is around infinite loops, where<p>void foo() { while (1) {} }<p>will loop forever, but<p>void foo(int i) { while (i) {} }<p>is permitted to terminate...even if i is 1:<p>&gt; An iteration statement whose controlling expression is not a constant expression, that performs no input&#x2F;output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate<p>To make things a bit worse, llvm can incorrectly <i>both</i> of the above terminate - <a href="https:&#x2F;&#x2F;bugs.llvm.org&#x2F;&#x2F;show_bug.cgi?id=965" rel="nofollow">https:&#x2F;&#x2F;bugs.llvm.org&#x2F;&#x2F;show_bug.cgi?id=965</a>.
评论 #13641031 未加载
评论 #13643177 未加载
评论 #13641956 未加载
评论 #13640949 未加载
评论 #13640901 未加载
DSMan195276over 8 years ago
I&#x27;ll be honest, I didn&#x27;t find any of these to be particularly surprising. If you&#x27;ve been using C and are familiar with strict-aliasing and common UB issues I wouldn&#x27;t expect any of these questions to seriously trip you up. Number 2 is probably the one most people are unlikely to guess, but that example has also been beaten to death so much since it started happening that I think lots of people (Or at least, the people likely to read this) have already seen it before.<p>I&#x27;d also add that there are ways to &#x27;get around&#x27; some of these issues if necessary - for example, gcc has a flag for disabling strict-aliasing, and a flag for 2&#x27;s complement signed-integer wrapping.
评论 #13640560 未加载
评论 #13640693 未加载
评论 #13642564 未加载
评论 #13643885 未加载
评论 #13640700 未加载
junk_disposalover 8 years ago
Honestly, Optimizing compilers will kill C.<p>It killed the one thing C was good at - simplicity (you know exactly what happens where, note I&#x27;m not saying speed, as C++ can be quite a bit faster than C).<p>Now, due to language lawyering, you can&#x27;t just know C and your CPU, you have to know your compiler (and every iteration of it!). And if you slip somewhere, your security checks blow up (<a href="http:&#x2F;&#x2F;blog.regehr.org&#x2F;archives&#x2F;970" rel="nofollow">http:&#x2F;&#x2F;blog.regehr.org&#x2F;archives&#x2F;970</a> <a href="https:&#x2F;&#x2F;bugs.chromium.org&#x2F;p&#x2F;nativeclient&#x2F;issues&#x2F;detail?id=245" rel="nofollow">https:&#x2F;&#x2F;bugs.chromium.org&#x2F;p&#x2F;nativeclient&#x2F;issues&#x2F;detail?id=24...</a>) .
评论 #13640760 未加载
评论 #13640807 未加载
评论 #13640870 未加载
评论 #13640950 未加载
评论 #13643963 未加载
评论 #13640761 未加载
Tharreover 8 years ago
I don&#x27;t think this Q&amp;A format makes for a good case of not knowing C.<p>I mean I got all answers right without thinking about them too much, but would I too if I had to review hundreds of lines of someone else&#x27;s code? What about if I&#x27;m tired?<p>It&#x27;s easy to spot mistakes in isolated code pieces, especially if the question already tells you more or less what&#x27;s wrong with it. But that doesn&#x27;t mean you&#x27;ll spot those mistakes in a real codebase (or even when you write such code yourself).
评论 #13640977 未加载
hermitdevover 8 years ago
It&#x27;s worth noting that for example #12, the assert will only fire for debug builds (i.e. the macro NDEBUG is not defined). So, depending on how the source is compiled, it may be able to invoke the div function with b == 0.
eon1over 8 years ago
C also: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12902304" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12902304</a>
userbinatorover 8 years ago
IMHO the problem is with compilers (and their developers) who think UB really means they can do <i>anything</i>, when what programmers usually expect is, and the standard even notes for one of the possible interpretations of UB, &quot;behaving during translation or program execution in a documented manner characteristic of the environment&quot;.<p>Related reading:<p><a href="http:&#x2F;&#x2F;blog.metaobject.com&#x2F;2014&#x2F;04&#x2F;cc-osmartass.html" rel="nofollow">http:&#x2F;&#x2F;blog.metaobject.com&#x2F;2014&#x2F;04&#x2F;cc-osmartass.html</a><p><a href="http:&#x2F;&#x2F;blog.regehr.org&#x2F;archives&#x2F;1180" rel="nofollow">http:&#x2F;&#x2F;blog.regehr.org&#x2F;archives&#x2F;1180</a> and <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8233484" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=8233484</a>
评论 #13640546 未加载
评论 #13640424 未加载
评论 #13640534 未加载
评论 #13640487 未加载
评论 #13640499 未加载
sparky_over 8 years ago
I suppose this sort of ambiguity is what drives the passion of Rust and Go programmers.
评论 #13640929 未加载
federicoponziover 8 years ago
Before: What? I know C. After 3 questions: Ok, I don&#x27;t know C. Well played sir.
E6300over 8 years ago
1. Unless C&#x27;s variable definition rules are completely different from C++&#x27;s, int i; is a full definition, not a declaration. If both definitions appear at the same scope (e.g. global), this will cause either a compiler error or a linker error. A variable declaration would be extern int i;
评论 #13640417 未加载
brianmurphyover 8 years ago
As a former C programmer, you know not to fool around at the max bounds of a type. That avoids all of the integer overflow&#x2F;underflow conditions. When in doubt, you just throw a long or unsigned on there for insurance. :)
nightcrackerover 8 years ago
I got every single one right. Does that mean I know C through and through? Perhaps. But all of these are the &#x27;default&#x27; FAQ pitfalls of C, not the really tricky stuff.
AndyKelleyover 8 years ago
I made this post as a response. Disclaimer: yet another programming language trying to dethrone C. People seem to be less enthusiastic about the subject these days.<p><a href="http:&#x2F;&#x2F;andrewkelley.me&#x2F;post&#x2F;zig-already-more-knowable-than-c.html" rel="nofollow">http:&#x2F;&#x2F;andrewkelley.me&#x2F;post&#x2F;zig-already-more-knowable-than-c...</a>
kvakkeflyover 8 years ago
Anyone who enjoys this will also enjoy <a href="http:&#x2F;&#x2F;cppquiz.org" rel="nofollow">http:&#x2F;&#x2F;cppquiz.org</a>
Hydraulix989over 8 years ago
I feel bad because I&#x27;m smart enough to answer these questions correctly in a quiz format<i></i>, but if I saw any of them in production code, I would not even think twice about it.<p><i></i> (the quiz questions themselves lead you on, plus I read the MIT paper on undefined behavior that was posted on here back in 2013)
rdc12over 8 years ago
Isn&#x27;t this line from #3, undefined behavior not mentioned in the article (sequence point violation)<p><i>zp++ = </i>xp + *yp;
评论 #13640828 未加载
wmuover 8 years ago
#4 is not really language issue, rather a floating point numbers feature.
raartsover 8 years ago
(2015)
Kenjiover 8 years ago
I&#x27;m sorry, but the answer this website gives to 1. is wrong. See for yourself:<p><pre><code> int i; int i = 10; int main(int argc, char* argv[]){ return 0; } </code></pre> Try to compile it. It doesn&#x27;t work (gcc.exe (GCC) 5.3.0), the error is:<p><pre><code> a.cc:2:5: error: redefinition of &#x27;int i&#x27; int i = 10; ^ a.cc:1:5: note: &#x27;int i&#x27; previously declared here int i; ^ </code></pre> Either I misunderstood the author and this example, or I do know C.
评论 #13643553 未加载