Sujet : Re: What is your opinion about unsigned int u = -2 ?
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.cDate : 31. Jul 2024, 21:29:48
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87mslxe4wj.fsf@bsb.me.uk>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13)
Thiago Adams <
thiago.adams@gmail.com> writes:
What is your opinion about this:
>
unsigned int u1 = -1;
>
Generally -1 is used to get the maximum value.
Yes, that's a common usage, though I prefer either -1u or ~0u.
Is this guaranteed to work?
>
How about this one?
>
unsigned int u2 = -2;
Does it makes sense? Maybe a warning here?
Warnings are almost always good, especially if they can be configured.
For example you can ask gcc to warn about converting -1 to unsigned
while leaving -1u and ~0u alone.
-- Ben.