Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: does a double cast to unsigned makes sense in any circumstance? Date: Thu, 20 Jan 2022 19:37:21 -0800 Organization: A noiseless patient Spider Lines: 19 Message-ID: <86bl05rc7i.fsf@linuxsc.com> References: <86czl8kaew.fsf@levado.to> <86bl0ctree.fsf@levado.to> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="9bbd2bd2338bc444f9d0c408d9cb7c1c"; logging-data="6527"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183CRo0Q3gVUjYFAT0YXIyys+/RnPsX0EU=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:NgwcLTJPN6+ybo6NRToSdLeh+M4= sha1:y/A+W/kSk9CtJPZQPCpSfF8vQcs= Xref: csiph.com comp.lang.c:164500 James Kuyper writes: > On 1/15/22 8:12 PM, Meredith Montgomery wrote: > ... > >> Interesting. I got another question here. If s[pos] is always a char >> and '0' is always a char, then s[pos] - '0' will never be greater than a >> char. It might be negative. If it's negative and if I do > > Note that while members of the basic character set are required to be > represented by non-negative values (6.2.5p3), members of the extended > character set are not. If char is signed, they can be as low as > CHAR_MIN, in which case subtracting '0' necessarily results in undefined > behavior. [...] No, it doesn't. Subtracting one character value from another can give undefined behavior, but it doesn't have to, and indeed usually cannot. The only circumstance where it can is when char is signed and CHAR_MAX == INT_MAX.