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.

C Questions and Answers

149 pointsby skazka16about 10 years ago

23 comments

ChuckMcMabout 10 years ago
I think this was silly, the author clearly does know C but they are complaining about optimizing compilers which do things &quot;behind your back&quot; and are becoming an increasing nuisance. It&#x27;s sort of a passive aggressive &quot;I think this should be an error but it isn&#x27;t an error because twisted logic that the compiler uses with respect to undefined operation.&quot;<p>That people can teach themselves what to expect the compiler to do isn&#x27;t all that surprising, and it also isn&#x27;t surprising that a &quot;modern&quot; compiler does stuff an &quot;old&quot; C programmer might think is ridiculous.<p>I&#x27;ve engaged in this particular argument a few times only to throw up my hands in frustration over some exquisitely twisted line of reasoning that gave the compiler hacker person a fraction of a percent improvement[1] by exploiting this kind of situation. As long as I have a compiler flag that turns it all off its tolerable. But sheesh, sometimes I think these are C programmers who don&#x27;t have the guts to become Rust programmers. You want to start fresh dudes, clean slate. Embrace it.<p>[1] &quot;But Chuck, over the millions of machines out there its like an entire computer&#x27;s worth of CPU cycles you can use for something else!&quot;
评论 #9143190 未加载
评论 #9143638 未加载
评论 #9143242 未加载
评论 #9143187 未加载
评论 #9147631 未加载
评论 #9146193 未加载
EpicEngabout 10 years ago
Well apparently I do know C. If the author wanted to be as contrived a possible there are certainly more devious edge cases which could have been trotted out. The fact that e.g. the compiler may optimize out a NULL check <i>after</i> you&#x27;ve already dereferenced the darn thing shouldn&#x27;t be surprising. Just fix your silly bug.
评论 #9143094 未加载
评论 #9143482 未加载
评论 #9143642 未加载
ternaryoperatorabout 10 years ago
This reminds of me of the Quiz books that were popular years ago. They&#x27;d show some code that inadvertently tripped some obscure corner of the language.<p>Rarely did the quizzes provide great insight. Rather, they confirmed the benefits of keeping your code idiomatic.
评论 #9143169 未加载
评论 #9143685 未加载
评论 #9143189 未加载
bgvopgabout 10 years ago
Surprisingly accurate, author did his research.<p>5 is also missing a check for a NULL pointer. ;)<p>Most of there rules are unfortunately ignored, as obscure information. The worst offender I see in wild code is 5.<p>The second most ignored is not checking values before computation: 10, 11, 12.<p>No.7 is very interesting, rarely violated, most programmers don&#x27;t even know that is a thing or just assume the processor won&#x27;t trap on an unaligned read.
评论 #9143447 未加载
ericfontaineabout 10 years ago
Although most comments use these examples to argue that C is a bad language, I would argue the opposite, that these examples show how C is an extremely useful language. C has occupied a niche position as the lowest commonly-used language that is both human-readable (at the level of expressing algorithms, data structures, functionality, and control flow) but not specific to any one instruction set architecture (and thus can compile to any architecture). Each C construct or statement maps efficiently into machine instructions, while not being an assembly language. I have trouble imagining how to make something like C any lower without becoming architecture-specific or cumbered with details that are more economically-suited for a compiler. But if too much higher, then the programmer becomes detached from this close relationship to the computer. The programmer can focus on implementing important speed &amp; space performance details of an algorithm while not getting bogged down by more mundane details (such as register allocation, matching jump statements to their targets labels, keeping track of the return stack of a function, etc.) that are better suited for a compiler to handle. (That being said, I think Rust or something like it is a strong successor and can additionally express concurrency).<p>These examples illustrate well-intended and useful features of C, not flaws. I will explain why for each in a comment below:
评论 #9144010 未加载
评论 #9144458 未加载
belovedeagleabout 10 years ago
&gt; (especially C programmers)<p>Author needs to stop his anti-intellectual everyone-is-as-ignorant-as-me bullshit. I see that a lot re programming to justify a lot of silly positions. If you only know Javascript, that&#x27;s great, I rather like having shiny things in my browser (I like it too much, even). That doesn&#x27;t mean that C programming is obsolete; some of us know C. For example, I got #5 and #9 wrong, and the rest I got right including the general idea of the justifications. (10&#x2F;12 is pretty good for someone who grew up in the Java era, but I want to get better.)
评论 #9143178 未加载
评论 #9143147 未加载
评论 #9143207 未加载
评论 #9143209 未加载
chadaustinabout 10 years ago
The only one I got wrong was the one about IEEE semantics. It clearly is possible to know C. :P
dllthomasabout 10 years ago
I&#x27;ll readily admit that there are corners of C where I would not be able to tell you just what breaks. But I know about where they are and enough to stay away from them. If I&#x27;m not sure, the next guy won&#x27;t be either.<p>I got 12&#x2F;12 here. I would say I know C.
Veedracabout 10 years ago
A trickier variant of #11 is to show people<p><pre><code> bool is_zero(int x) { return x == -x; } bool is_zero(float x) { return x == -x; } </code></pre> and ask them which is wrong and for what value. Most of the time the instinctive response is that it must be the float code (because floats are evil, duh).<p>This works even in languages with defined overflow for integers.
评论 #9143102 未加载
copsarebastardsabout 10 years ago
1.<p><pre><code> int i = 10; </code></pre> Q. Is this code correct?<p>A. Yes.<p>2.<p><pre><code> extern void bar(void); void foo(int *x) { if(x == NULL) { return; } int y = *x; bar(); return; } </code></pre> Q. It turns out if you check the validity of your variables before you use them it prevents you from having to understand undefined behavior.<p>A. Is there a question here?<p>3. There was a function:<p><pre><code> #define ZP_COUNT 10 void func_original(int *xp, int *yp, int *zp) { int i; for(i = 0; i &lt; ZP_COUNT; i++) { *zp++ = *xp + *yp; } } </code></pre> I optimized it this way:<p>...because nobody had any idea what it was doing and so it wasn&#x27;t used anywhere.<p>4.<p><pre><code> double f(double x) { assert(x != 0.); return 1. &#x2F; x; } </code></pre> Q. Is it possible for this function to return inf?<p>A. If you&#x27;re at the point where you&#x27;re asking that question you should have been using a decimal library a long time ago.<p><pre><code> int my_strlen(const char *x) { int res = 0; while(*x) { res++; x++; } return res; } </code></pre> Q: The provided above function should return the length of the null-terminated line. Find a bug.<p>A. They didn&#x27;t use `strlen()`.
_RPMabout 10 years ago
This reminds me of tests I took in earlier CS classes. Knowing those things are utterly useless in practice.
评论 #9143052 未加载
评论 #9143193 未加载
评论 #9142866 未加载
评论 #9143259 未加载
评论 #9143013 未加载
评论 #9143111 未加载
sheleabout 10 years ago
Regarding the first answer: What is called a &quot;tentative definition&quot; is of course a &quot;declaration&quot;.
评论 #9143515 未加载
评论 #9144061 未加载
评论 #9143485 未加载
belovedeagleabout 10 years ago
Why the heck was the name of this post changed? It rather conveniently puts the author in a better light by downplaying the anti-intellectual nature of the article I commented about above. I thought the general rule was that posts should be titled with the title of the linked article, which was the case before but now is not the case.<p>EDIT: To answer my own question, the submitter is clearly the author based on his submission history. So yes, this was an act of self-censorship to try to hide the author&#x27;s disgusting attitudes.
评论 #9148184 未加载
评论 #9147147 未加载
kdohertyabout 10 years ago
Unsigned int &gt;= 0 in a decrementing for-loop is a classic trap
评论 #9143066 未加载
seba_dos1about 10 years ago
5 is IMO nothing else than nitpicking. 2 and 4 (and maybe 6) might have some importance in real life, while others are easy and kinda expected (although comma operator in 8 might not be known to less experienced programmers). I can&#x27;t really see the point of this article other than &quot;hey, do you remember that there is a concept called undefined behavior in C?&quot;.
评论 #9149771 未加载
nspattakabout 10 years ago
I was so surprised by the first question that I actually tried it on my computer...<p>Am I the only one who tried compiling :<p>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; int main(int argc, char *argv[]) { int i; int i=10; printf(&quot;i=%d\n&quot;, i); return 0; }<p>and got the redeclaration error I expected?
评论 #9143704 未加载
kenjacksonabout 10 years ago
I just got the first one wrong and haven&#x27;t written C code in a decade. I have to admit I haven&#x27;t seen that construct used in any programs. I guess, no harm no foul, but still seems like an odd construct to allow.
评论 #9147453 未加载
ConAntonakosabout 10 years ago
I realize this has probably been asked, but anyone recommend good resources for learning C? And not necessarily just the legendary textbooks, but any clever tutorials or fun learning resources? Thanks!
bottled_poeabout 10 years ago
Does anyone know of a tool which can automatically scan source code for these types of oversights?<p>Assuming of course the compiler doesn&#x27;t already check for all of these...
评论 #9143087 未加载
halayliabout 10 years ago
Knowledge is not absolute. Yes you can be a C expert and still not know every single little detail about C or any other language for that matter.
dangabout 10 years ago
Since the title seems to be controversial we changed it to a neutral one.
fizixerabout 10 years ago
He&#x27;s discussing subtle points of the C language and yet there is no mention of which compiler he&#x27;s using, whether the results might be different for different compilers, hardware platforms, and how they correlate with multiple C standards (C89, C99, C11&#x2F;C1X, etc).<p>He succeeded in convincing me that he does not know C!
评论 #9142987 未加载
评论 #9143117 未加载
评论 #9147569 未加载
fdikabout 10 years ago
Already the first example is wrong. So I stopped reading.<p>% cc -std=c11 -o dingens dingens.c dingens.c:6:9: error: redefinition of &#x27;i&#x27; int i = 10; ^ dingens.c:5:9: note: previous definition is here int i; ^ 1 error generated. % cat dingens.c<p>#include &lt;stdio.h&gt;<p>int main() { int i; int i = 10;<p><pre><code> printf(&quot;hello, world\n&quot;); return 0;</code></pre> }<p>%
评论 #9143079 未加载
评论 #9143110 未加载