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.

What is “:-!!” in C code?

242 pointsby frontforover 8 years ago

11 comments

Mindless2112over 8 years ago
That code seems needlessly arcane. You can get the same results without resorting to anonymous bitfields:<p><pre><code> #define BUILD_BUG_ON_ZERO(e) (sizeof(char[(e) ? -1 : 0])) </code></pre> which, when it fails, results in<p><pre><code> error: size of unnamed array is negative </code></pre> which is no worse than the provided code which results in<p><pre><code> error: negative width in bit-field &#x27;&lt;anonymous&gt;&#x27; </code></pre> It&#x27;s worth noting that declaring an array with 0 elements is not allowed in C99. However, using a struct with no named members has undefined behavior in C99 [1].<p>[1] <a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;12918937&#x2F;959866" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;12918937&#x2F;959866</a><p>Edit: You can get around having 0 elements by using<p><pre><code> #define BUILD_BUG_ON_ZERO(e) (sizeof(char[(e) ? -1 : 1]) - 1) </code></pre> but you&#x27;re starting to lose clarity again.
评论 #13003671 未加载
评论 #13004489 未加载
olalondeover 8 years ago
And this is why I hated learning C. You went through a 500 pages C programming book, did all the exercises, had a good grasp of the language but you were still useless because you didn&#x27;t know anything about those weird macros, Makefiles, automake, gcc flags, etc.
评论 #13003508 未加载
评论 #13003285 未加载
评论 #13003279 未加载
评论 #13003871 未加载
评论 #13003337 未加载
评论 #13003489 未加载
评论 #13003400 未加载
comexover 8 years ago
Note that C11 has a more friendly static assertion feature:<p><pre><code> _Static_assert(condition, &quot;error message if condition evaluates to false&quot;); </code></pre> (You can also use &quot;static_assert&quot; as an alias for the ugly keyword after including &lt;assert.h&gt;.)
评论 #13003438 未加载
an_account_nameover 8 years ago
Ah, neat. A version of static_assert for C. What kinds of expressions can it actually evaluate, though?
评论 #13003298 未加载
lordnachoover 8 years ago
Here&#x27;s another one.<p><a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1642028&#x2F;what-is-the-name-of-the-operator-in-c" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1642028&#x2F;what-is-the-name-...</a><p>int main() { int x = 10; while (x --&gt; 0) &#x2F;&#x2F; x goes to 0 { printf(&quot;%d &quot;, x); } }
jzymbalukover 8 years ago
Every time I feel pretty good about my knowledge of C, something like this comes along and reminds me I don&#x27;t know anything at all.
thanatropismover 8 years ago
Semi related to the &quot;is C ugly&quot; debate.<p>I know Fortran 90 and use it for some simulations -- since it has a matrix syntax it&#x27;s not that hard to write changing matlab code. But I know Fortran is going where the wild roses grow, and wonder if I should spend the time to learn C for high-dimensional numerics.
评论 #13006097 未加载
muyuuover 8 years ago
The question adds confusion to it, should have put the whole macro to give it context.
mkesperover 8 years ago
Rust discussion of a similar feature: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;issues&#x2F;6676" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;issues&#x2F;6676</a>
评论 #13003906 未加载
评论 #13003893 未加载
olliejover 8 years ago
It&#x27;s a person misreading syntax :-&#x2F;
austincheneyover 8 years ago
The next time developers cry about JavaScript&#x27;s incompetence because of potential hacky type coercion stupidity I will shove this in their face.
评论 #13005177 未加载