On Sat, May 03, 2014 at 06:14:04PM +0200, Electron wrote: >=20 > Hello, >=20 > At 17.29 2014.05.02, Bob Ammerman wrote: > >Yes, many compilers will give you a warning about comparing signed to > >unsigned. You really just shouldn't do it! > > > >If you think about what happens at the hardware level, where the compari= son > >is basically a subtraction, you can't properly interpret the flags (C a= nd Z > >on a PIC) set by the comparison. >=20 > Yes, but (when comparing a signed int to an unsigned int) I don't underst= and > why the standard was decided to cast the signed int to an unsigned it ins= tead > of the other way round, which would not be perfect but would be more logi= cal > to me. It would have still been as equally broken in the opposite direction: int a =3D -100 unsigned int b =3D 65435; // This is -101 signed if (a < b) // Signed would compare -100 and -101. This would fail as false.= ... Simply put the standard had to make an arbitrary choice. >=20 > Also, it would have been possible to implement such comparisons, although= at > the expense of extra code being generated (but then again it would have o= nly > happened when the programmer was comparing signed and unsigned, so not al= ways). Standards are for the most part a codification of practice. The first ANSI C standard was published in 1989. At that point C language practice was over 20 years old with millions of lines of code already in the field. No compiler implemented those extra comparisons. So they were not coded into the standard. BAJ >=20 > Cheers, > Mario >=20 >=20 > > > >-- Bob Ammerman > >RAm Systems > > > > > >-----Original Message----- > >From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf= Of > >Electron > >Sent: Friday, May 02, 2014 10:43 AM > >To: Microcontroller discussion list - Public.; Microcontroller discussio= n > >list - Public. > >Subject: [OT] Why on earth??!? > > > > > >Hello! > > > >a is a signed int and contains -100 > >b is an unsigned int and contains 500 > > > >a > > >Why on earth? :P > > > >I can cast b to signed int and get the correct results, but leaving as i= s > >instead the result surprises me a lot, as I have no idea why -100<500 sh= ould > >be false, it's like if the compiler casts a to an unsigned type > >automatically (and this was clearly not requested by the programmer). > > > >If we keep them as they are, i.e. the first signed and the second unsign= ed, > >then why should a > > >This is very confusing really. > > > >Now I have to correct all of my code, looking for comparisons between si= gned > >and unsigned ints, and cast both variables to the type I mean. :-/ > > > >Is there any other situation I have to be careful about when mixing sign= ed > >and unsigned types? > > > >Please do not reply the obvious "generically the use of unsigned types i= s > >not adviceable, why don't you stick with only signed types? you will be = much > >safer". THANKS. > > > >Cheers, > >Mario > > > >-- > >http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/ch= ange > >your membership options at http://mailman.mit.edu/mailman/listinfo/picli= st > > > >--=20 > >http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > >View/change your membership options at > >http://mailman.mit.edu/mailman/listinfo/piclist >=20 > --=20 > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist --=20 Byron A. Jeff Chair: Department of Computer Science and Information Technology College of Information and Mathematical Sciences Clayton State University http://faculty.clayton.edu/bjeff --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .