Sujet : Re: if(!(i%16))
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 31. Mar 2024, 00:34:12
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <uua7hj$19ih8$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 30/03/2024 22:49, fir wrote:
bart wrote:
On 30/03/2024 21:44, fir wrote:
void bytes_dump_in_hex()
{
for(int i=0; i<bytes_size; i++)
{
if(!(i%16)) printf("\n");
>
printf("%02x ", bytes[i]);
}
}
>
in the code above seem that those inner () in if(!(i%16)) are needed
>
why is that so?
>
Without them !i%16 will be parsed as (!i)%16.
>
and does it have sense?
It's a unary operator. Usually they are applied before binary ones. So '++ p + n' means '(++p)+n' not '++(p+n)' which wouldn't work anyway.
And -2-3 means (-2)-3 or -5, not -(2-3) which would be +1.
However whether it makes sense is beside the point. It's how C has always worked.
if ! is boolean operator and % is arithmetic then converting things to bolean and then do arithmetoc on it seems not much reasonable...
Some people think that ! should work like that, so that here:
not a<b and b<c
the 'not' applies to the entire expression: not(a<b and b<c), rather than (not a)<b and b<c.
But honestly, there are lots of things that are worse about C's set of precedences.