From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,a9b0810d3106d9b8 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Fun with C Date: Sat, 16 Apr 2011 23:12:37 +0200 Organization: A noiseless patient Spider Message-ID: <8762qdyk6y.fsf@ludovic-brenta.org> References: <27cf3992-4132-4483-9110-adc7a089cd4a@e8g2000vbz.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx03.eternal-september.org; posting-host="hdEhN5XwWWNWyggynB6xnQ"; logging-data="10592"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xbvTZyi4xfD7IRr7VxSKy" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Cancel-Lock: sha1:jCRtSRvojtkaYJtH2E6Ul6+LSz0= sha1:GGXoQlfpvKCaUsxGAr4rYt8peXQ= Xref: g2news2.google.com comp.lang.ada:19793 Date: 2011-04-16T23:12:37+02:00 List-Id: "Nasser M. Abbasi" writes: > On 4/16/2011 10:02 AM, George P. wrote: >> Spend few days chasing bug in embedded C project which can drilled >> down to this. Consider the following program, and all reasonable men >> will expect the result to be negative. Not in C, and it is not a >> compiler bug :-( >> >> And offcorse none of the three comilers I cheched did not bother to >> issue a warning at least.. >> >> #include >> >> int main() >> { >> unsigned n = 128; >> int i = -2048; >> int r; >> >> r = i / n; >> >> printf("R = %d\n", r); >> >> return 0; >> } > > > $ gcc -Wall t.c > $ ./a.out > R = 33554416 So R = 2**25 - 16, the correct answer is -16, so the actual result is the correct one except for bit 24, which is 1 when it should be zero. Out of curiosity, is this actual result a predictable consequence of the language definition, or is it undefined behavior? -- Ludovic Brenta.