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 '<anonymous>'
</code></pre>
It'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://stackoverflow.com/a/12918937/959866" rel="nofollow">http://stackoverflow.com/a/12918937/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're starting to lose clarity again.
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't know anything about those weird macros, Makefiles, automake, gcc flags, etc.
Note that C11 has a more friendly static assertion feature:<p><pre><code> _Static_assert(condition, "error message if condition evaluates to false");
</code></pre>
(You can also use "static_assert" as an alias for the ugly keyword after including <assert.h>.)
Here's another one.<p><a href="http://stackoverflow.com/questions/1642028/what-is-the-name-of-the-operator-in-c" rel="nofollow">http://stackoverflow.com/questions/1642028/what-is-the-name-...</a><p>int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}
Semi related to the "is C ugly" debate.<p>I know Fortran 90 and use it for some simulations -- since it has a matrix syntax it'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.
Rust discussion of a similar feature: <a href="https://github.com/rust-lang/rust/issues/6676" rel="nofollow">https://github.com/rust-lang/rust/issues/6676</a>