Re: [gtk-list] Re: unary minus operator applied to unsigned type,result still unsigned



On Wed, 17 Feb 1999, Tor Lillqvist wrote:

> > could you give the output of
> > [ sample program ]
> 
> Yes, this prints the same output when compiled with MS C. Actually
> that sample program doesn't even trigger the warning. Apparently the
> warning is given only if unary minus is applied to an unsigned int,
> which is being assigned to an int (or passed as an int).
> 
> As the end result seems to be the same as with gcc, I guess I can
> safely ignore these warnings. Still, IMHO, it would make the intended
> effect clearer to the human reader of the program if an (int) cast
> were used when an unsigned value is being negated.
> 
> (Note that if you use unsigned int instead of unsigned short in the
> sample program, you get the output - 5 = 4294967291.000000, also with gcc)

erk, i should have been more comprehensive in that test program.

#include <stdio.h>
int
main ()
{
  unsigned short s = 5;
  unsigned int ui = 5;
  int i = - ui;
  double d1 = - s;
  double d2 = - ui;

  printf ("- %u = %f = %f = %d\n", s, d1, d2, i);

  return 0;
}

in fact, gives:

$ gcc -Wall -ansi -pedantic unary-minus.c && ./a.out
- 5 = -5.000000 = 4294967291.000000 = -5

so the thing that saved us in those code portions is that we were
usually using ints as lvalue and then the value "wraps around" so
we get the proper negative value again (as with i = - ui; in the above).

as a result, we should change those code portions and someone should file
a feature request to the egcs people about -Wunsigned-unary-minus (at least
i couldn't find a -Woption for gcc that would trigger the MSC warning).

> 
> --tml
> 

---
ciaoTJ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]