top | item 36627419

(no title)

romnon | 2 years ago

ive seen people doing += !!(c=='s')-!!(c=='p') for that

discuss

order

sweetjuly|2 years ago

I'm sure people do that (even though it's not necessary per some year C standard) but generally the pattern is actually for converting things which are not already 0 or 1 into 0 or 1. For example, you might want to use it here:

    int num_empty_strings = !!(strlen(s1)) + !!(strlen(s2)) + !!(strlen(s3))
which is equivalent to:

    int num_empty_strings = (strlen(s1) != 0) + (strlen(s2) != 0) + (strlen(s3) != 0)
Which you use is really a matter of coding style.

stkdump|2 years ago

If we are being cryptic already, why not

    int num_empty_strings = !!*s1 + !!*s2 + !!*s3;

areyousure|2 years ago

Note that your code computes the number of nonempty strings.

simonkagedal|2 years ago

That is entirely unneccessary. An == expression will always evaluate to 1 or 0. The !!x trick can be useful in some other situations, though.

Here’s a thing you could do (but I don’t know why):

+= !(c-’s’) - !(c-’p’)