As others have pointed out, the purpose is to guarantee that the truthy value is 1 instead of all possible nonzero values.
This can prevent bugs where people rely on true being 1, leading to bugs like:
env = getenv("POSIXLY_CORRECT")
if (env == true) {
// handle the true case
}
Engineers would use !! as a defense against bad coders. Yes, it is better to not write buggy code, but it's almost as good to ensure that the bugs never bit you.
Note that comparing a boolean to true is still Bad Coding (tm), even if you are using the !! trick. There is no reason for any test beyond if (bool_var)
. And the example given is suboptimal as well, as it relies on NULL being 0, which might not be the case on all architectures. The code you give really ought to be
d->__posixly_correct = posixly_correct || getenv("POSIXLY_CORRECT") != NULL;
to defend against the case where 0 is a legitimate memory address and happens to be where the getenv()'s return is stored