Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How is he doing: int a[2] = { 1, 2 }; printf("%d\n", 2[a]); Date: Sun, 01 Jan 2017 13:09:48 -0800 Organization: None to speak of Lines: 61 Message-ID: References: <87y3yxinjl.fsf@bsb.me.uk> <871swpiiu8.fsf@bsb.me.uk> <5868defa.4980093@news.xs4all.nl> <87ful3q9bt.fsf@bsb.me.uk> <87a8bbq6ef.fsf@bsb.me.uk> <87r34mpz0g.fsf@bsb.me.uk> <878tqupule.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="8719ec07cc9cf870a68c352ae34a5c70"; logging-data="6389"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX186SmwOAWjybAjMI5vz7ofX" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:HmhigXpI3hlrVEiIBiVsLsAqMZk= sha1:0uSrUmZMu4Da2ovbh3z7wLgq9W0= Xref: csiph.com comp.lang.c:100814 Ben Bacarisse writes: > BartC writes: >> On 01/01/2017 15:42, Ben Bacarisse wrote: >>> BartC writes: > >>>> Yet if you negate a _Bool with a value of 1, you're liable to end >>>> up with 4294967295. >>> >>> On what compiler? You need to report this as a bug. >> >> Assigning to a bool is OK (the value is adjusted to be 0 or 1). But >> anything else doesn't seem to be adjusted the same way: >> >> _Bool a,b; > >> a = 1; > >> printf("-a = %u\n",-a); > >> Output is the same on all compilers: > >> -a = 4294967295 > > What do you expect -1 to print as using the %u format? (Technically, > the code is undefined but I don't think any implementation takes > advantage of that permission.) > >> So it seems to hinge on what the type of '-a' is intended to be. It >> doesn't look like it's _Bool. > > When a is of type _Bool with value 1, -a has the value -1. It's not > rocket science, but it is C and you are constantly puzzled by C's rules. > The bottom line is that negating a _Bool with value 1 does not give you > 4294967295. You are spreading more confusion by suggesting that you are > likely to get that value. Directly negating a _Bool is not possible; there is no unary "-" operator that takes an operand of type _Bool (or of any integer type narrower than int). Given: _Bool a = 1; the expression -a converts the value of a to type int, and then negates it, yielding a result of type int with the value -1. Printing that int value with a "%u" has undefined behavior, but will typically print an unsigned int value whose representation is the same as the int value -1, often 4294967295. The fact that C's arithmetic operators do not apply to narrow types, requiring moderately complicated rules to promote the operands, can cause some confusion. (Let me suggest yet again that the solution to such confusion is to understand the rules.) Why would you want to apply a unary "-" operator to a _Bool operand in the first place? -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"