[cfe-dev] Unexpected behaviour for constant expression initializer

Frits van Bommel fvbommel at gmail.com
Thu Oct 21 06:51:03 PDT 2010


On Thu, Oct 21, 2010 at 3:01 PM, Arnaud Allard de Grandmaison
<Arnaud.AllardDeGrandMaison at dibcom.com> wrote:
> The following reduced C testcase :
>
> $ cat initializer.c
>
> unsigned long ok = 8509568; // 0x81D880
>
> unsigned long bogus = 0x800000 + 2*3780*16; // 0x81D880

I'm pretty sure the small integers in those initializers are of type
int until they're "assigned" to the unsigned long global and/or used
in arithmetic with an integer constant with a larger[1] type. So if
'int' is something like i16 (which I'm guessing is the case for
MSP430?), the intermediate value 2*3780*16 will overflow before being
added to 0x800000 (which doesn't fit in i16, so it's probably
automatically a long int).
Try appending 'L' (or 'UL') to the 2, 3780 and/or 16.


[1] (Where long > int, even if both are 32 bit)



More information about the cfe-dev mailing list