This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: gcc warnings


Hi Jiri,

It's valid C code to assign from a variable of greater type to one of
smaller type.

I think that this is very unfortunate.  I presume you do as well.  Alas.

You can act upon it programmatically, in runtime, if you do something like:

#include "stdio.h"

unsigned char LongAsUnsignedChar(long c)
{
  if(c > 255 || c < 0)
  {
    fprintf(stderr, "Error: %ld outside of 0,255 range\n", c);
  }

  return c;
}

int main()
{
? long a = 10000;
? unsigned char b = LongAsUnsignedChar(a);

? printf("%d\n", b);

? return b;
}

Alternatively, you can use a B&D language like Ada where such assignments
cannot happen silently and need explicit conversion.

HTH,
--Eljay


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]