Authored by Adrien Oliva

Build bug on zero



Force a compilation error if condition is true, but also produce a result (of value 0 and type size_t), so the expression can be used e.g. in a structure initializer (or whereever else comma expressions aren't permitted).


sizeof(struct { int: -!!(e); }))

  • (e) compute expression e
  • !!(e) Logically negate twice: 0 if e == 0, otherwise 1
  • -!!(e) numerically negate the expression from step 2

If it was 0, then we declare a struct with an anonymous integer bitfield that has width zero. Everything is fine and we proceed as normal. On the other hand, if it is not zero, then it will be some negative number. Declaring any bitfield with negative width is a compilation error.

bug_on_zero.c 132 Bytes
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment