Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Constants: when to use L U UL ....

Reply
Thread Tools

Constants: when to use L U UL ....

 
 
luke
Guest
Posts: n/a
 
      09-21-2005
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

luke

 
Reply With Quote
 
 
 
 
Alexei A. Frounze
Guest
Posts: n/a
 
      09-21-2005
"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.
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.

Alex


 
Reply With Quote
 
 
 
 
Jirka Klaue
Guest
Posts: n/a
 
      09-21-2005
Alexei A. Frounze:

> 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


I don't think so. Since 0xffff is in the range of unsigned int on
a 16-bit-int system, you should get 65535.

Jirka
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-21-2005
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

 
Reply With Quote
 
luke
Guest
Posts: n/a
 
      09-21-2005
thanx for the answer, but I didn't understand why "long x = 0xFFFF"
produces -1
a long is 32 bit even on 16-bit systems, isn't it? so you get 65535
anyway

 
Reply With Quote
 
Alexei A. Frounze
Guest
Posts: n/a
 
      09-21-2005
"Eric Sosman" <> wrote in message
news:qZednaXtafzYzKzeRVn-...
> Alexei A. Frounze wrote:
> > 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.


OK, I was wrong. But I remember I had some problem with this or something
very similar... But maybe it was a bit different thing (signed to unsigned
conversion), though...
Sorry.

> > 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.


You're right about the UB (as prescribed by the standard), though it's more
likely to get this problem w/o any side effects other than just a wrong
value. Actually, I'd probably prefer to get an exception as form of this UB
to locate the problematic place in the code.

Alex


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      09-21-2005
On Wed, 21 Sep 2005 06:01:23 -0700, luke wrote:

> thanx for the answer, but I didn't understand why "long x = 0xFFFF"
> produces -1


It doesn't.

> a long is 32 bit even on 16-bit systems, isn't it? so you get 65535
> anyway


However 0xFFFF doesn't have long type, it has type int of that can
represent 65535 or else unsigned int which is guaranteed to be able to
represent 65535. In either case the value is then converted to long before
being assigned.

So the code does do the right thing i.e. x ends up with the value 65535,
always. But there are types other than long involved. However

long x = 0x10000;

STILL works find and is guaranteed to give x the value 65536. If the value
can't be represented as an unsigned int the compiler will represent it as
a long. However there are cases where you need to force to a longer type,
e.g. the expression

1 << 24

i.e. 1 left shifted 24 places is invalid on systems with, say, 16 bit
ints. Whereas

1L << 24

and for many cases

1UL << 24

is even better because bit manipulations are best done on unsigned types.

Lawrence









 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-21-2005


Alexei A. Frounze wrote On 09/21/05 10:05,:
> "Eric Sosman" <> wrote in message
> news:qZednaXtafzYzKzeRVn-...
>
>>Alexei A. Frounze wrote:
>>
>>>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.

>
>
> OK, I was wrong. But I remember I had some problem with this or something
> very similar... But maybe it was a bit different thing (signed to unsigned
> conversion), though...
> Sorry.


On many 16-bit systems, `long x = (int)0xFFFF;' would
initialize x to -1. More plausibly,

int i = 0xFFFF;
long x = i;

is likely (but not guaranteed, of course) to do the same.

>>>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.

>
>
> You're right about the UB (as prescribed by the standard), though it's more
> likely to get this problem w/o any side effects other than just a wrong
> value. Actually, I'd probably prefer to get an exception as form of this UB
> to locate the problematic place in the code.


IIRC (it was long ago), the IBM s/360 could be operated in
a mode that produced traps when integer arithmetic overflowed.
I don't know whether that mode survives in its s/390 -- er,
excuse me, "eServer zSeries" descendant.

--


 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      09-21-2005
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;

<snip>

Here you should use 4L instead of 4 as the expression a*4 is
calculating
using int arithmetic. If int is 16-bit, then c may not get the value
40000.

--
Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not use ''; file already in use. M K ASP .Net 11 04-09-2008 11:35 AM
where to use CPLD & where to use FPGA? kulkarku@math.net VHDL 6 03-06-2006 07:27 AM
How do I know when to use the Viewstate and when to use the posted data? :-) Simon ASP .Net 1 11-09-2004 02:32 AM
Can I use XPath or something to a remote Mac or Linux box and just query an xml file, not using web services and use encyrption? jake ASP .Net 0 07-06-2004 02:16 PM
Cannot use the profile "default" because it is in use, not. please.post@yur.re.ply Firefox 1 07-04-2004 03:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57