This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: gcc warnings
- From: John Love-Jensen <eljay at adobe dot com>
- To: "Humpolicek, Jiri - Acision" <jiri dot humpolicek at acision dot com>, MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Wed, 17 Oct 2007 06:41:47 -0500
- Subject: 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