What does it mean ":-!!" in C language?


In a Linux kernel library, specifically this: /usr/include/linux/kernel.h , there is a macro with code that is strange to me:

/* 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 where-ever else comma expressions
   aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))

What does it mean ":-!!" in C language?


O !! is an old trick to convert numeric values ​​to Boolean.

  • The first one ! negates the value, if it is 0 becomes 1 , if it is anything else it becomes 0 .
  • The second ! deny it again

The next part is to use this when defining a bit field in a struct .

When you define an integer numeric field in a struct can tell how many bits it will occupy:

struct Fields {
    int a: 6; //Ocupa 6 bits
    int b: 2; //Ocupa 2 bits

And the number of bits cannot be negative. Hence the - before the !! : If e is different from zero we would have a bit field with negative size, which gives a compilation error. If e is zero we will have a zero-length bit field, which is valid, although it doesn't do much good. But since it's all a set of macros to check constants at compile time it turns out to be useful.

Scroll to Top