linux - [Solved-3 Solutions] What is “-!!” in C code - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem:

What is “-!!” in C code ?

Linux - Solution 1:

Expression:

sizeof(struct { int: -!!(e); }))
click below button to copy the code. By - Linux tutorial - team
  • (e): Compute expression e.
  • !!(e): Logically negate twice: 0 if e == 0; otherwise 1.
  • -!!(e): Numerically negate the expression from step 2: 0 if it was 0; otherwise -1.
  • struct{int: -!!(0);} --> struct{int: 0;}: If it was zero, then we declare a struct with an anonymous integer bitfield that has width zero. Everything is fine and we proceed as normal.
  • struct{int: -!!(1);} --> struct{int: -1;}: On the other hand, if it isn't zero, then it will be some negative number. Declaring any bitfield with negative width is a compilation error.

Linux - Solution 2:

A few people seem to be confusing these macros with assert().

These macros implement a compile-time test, while assert() is a runtime test.

Linux - Solution 3:

  • It's creating a size 0 bitfield if the condition is false, but a size -1 (-!!1) bitfield if the condition is true/non-zero.
  • In the former case, there is no error and the struct is initialized with an int member.

Related Searches to - linux - linux tutorial - What is “-!!” in C code