Alexei A. Frounze wrote:
> "luke" <> wrote in message
> news: oups.com...
>
>>Hi,
>>I know that the suffixes L U UL mean that the constant has to be
>>treated as long, unsigned,... but I didn't know when to use it and when
>>not.
>>thanks
>
>
> A few examples:
>
> 1. long x = 0xFFFF;
> 2. int a=10000; long c = a*4;
>
> 1.: On a 16-bit system (int is 16-bit long) you'll get x=-1, while on 32-bit
> one you'll get x=65535... The fix is to write 0xFFFFU or 0xFFFFUL.
No, x will be 65535 on all conforming C implementations.
On a system with 16-bit int, 0xFFFF is an unsigned int constant
with the value 65535u. On a system with wider int, 0xFFFF is
an int constant with the value 65535. Either way, x is initialized
with the value 65535(u), converted to long. See 6.4.4.1/5.
> 2.: On a 16-bit system you'll get an overflow in multiplication and hence
> wrong value in c (less than 3276
, while on 32-bit the result will be OK.
> The fix is to cast either of multipliers to long or to turn 4 into 4L.
You're right about the overflow, but wrong about the
result: the behavior is undefined, and there is no guarantee
that any value (wright or rong) will be produced. The suggested
fix is correct.
--
Eric Sosman
lid