long vs unsigned long 
Author Message
 long vs unsigned long

I was having a little trouble programming in C on my Amiga 4000.

I had two functions, something like this:

void first(long index)
{
  long count=-1;
  do
  {
    /* do stuff */
    count++;
  }
  while (count<index);

Quote:
}

void second()
{
  unsigned long index;
  /* do stuff */
  first(index);

Quote:
}

Then I got paranoid and changed "index" in second() to long. Was it
necessary, or was I overly paranoid?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #80 D+ ADA N+++ |
| http://www.*-*-*.com/ ~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"To doo bee doo bee doo."
   - Frank Sinatra



Wed, 29 Jan 2003 03:00:00 GMT  
 long vs unsigned long

wrote in comp.lang.c:

Quote:
> I was having a little trouble programming in C on my Amiga 4000.

> I had two functions, something like this:

> void first(long index)
> {
>   long count=-1;
>   do
>   {
>     /* do stuff */
>     count++;
>   }
>   while (count<index);
> }

> void second()
> {
>   unsigned long index;
>   /* do stuff */
>   first(index);
> }

> Then I got paranoid and changed "index" in second() to long. Was it
> necessary, or was I overly paranoid?

That really depends on the range of values used.

The signed and unsigned varieties of each integral type in C have
certain specified properties.  One is that they are exactly the same
size.  Another is that for positive values that fit in the signed type
the representation is identical for both types.  That is, any value
between 0 and LONG_MAX (defined in <limits.h> has the identical bit
representation in both a signed or unsigned long.

A problem can occur if an unsigned long contains a value larger than
LONG_MAX (on implementations where ULONG_MAX > LONG_MAX).  The result
of converting the unsigned long value to a long value is
implementation defined.  The resulting value could be quite different
than you expect and cause your loop to execute a different number of
times than you expect, or to experience signed integral overflow that
causes undefined behavior.

Jack Klein
--
Home: http://jackklein.home.att.net
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.faqs.org/faqs/C-faq/learn/



Wed, 29 Jan 2003 03:00:00 GMT  
 long vs unsigned long

Quote:
>Well, in practice it would only make a difference if 1) the default type
>for your platform is signed;

???

The type of his variable is *long*, not char!!!  long is a signed type
in C.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Wed, 29 Jan 2003 03:00:00 GMT  
 long vs unsigned long

Quote:
>void first(long index)
>{
>  long count=-1;
>  do
>  {
>    /* do stuff */
>    count++;
>  }
>  while (count<index);
>}

>void second()
>{
>  unsigned long index;
>  /* do stuff */
>  first(index);
>}

>Then I got paranoid and changed "index" in second() to long. Was it
>necessary, or was I overly paranoid?

The compiler is required to convert the argument of first() to the type
expected by first(), because first's prototype definition is in scope.  
So, you could define index in second() as any arithmetic type suitable
to you.  The only requirement is that its value, when passed to
first(), is within the range of values that can be represented in a
long.

So, the answer to your question is that you were overly paranoid :-)

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Wed, 29 Jan 2003 03:00:00 GMT  
 long vs unsigned long


Quote:
>I was having a little trouble programming in C on my Amiga 4000.

>I had two functions, something like this:

>void first(long index)
>{
>  long count=-1;
>  do
>  {
>    /* do stuff */
>    count++;
>  }
>  while (count<index);
>}

>void second()
>{
>  unsigned long index;
>  /* do stuff */
>  first(index);
>}

>Then I got paranoid and changed "index" in second() to long. Was it
>necessary, or was I overly paranoid?

Well, in practice it would only make a difference if 1) the default type
for your platform is signed; and 2) index can take values greater than
LONG_MAX. If that's the case, casting to signed is implementation-
defined, but most likely would result in a large _negative_ value.

A better thing to do, if index can regularly take very large values,
might be to rethink how the first() function works.

--
Hong Ooi                    | Centre for Maths and its Applications/

Ph: (02) 6267 4140          | Australian National University
                            | ACT 0200 Australia



Thu, 30 Jan 2003 03:00:00 GMT  
 long vs unsigned long
On Sun, 13 Aug 2000 03:41:40 +1000, that hoopy frood Hong Ooi
scribbled the following:

Quote:


>>I was having a little trouble programming in C on my Amiga 4000.

>>I had two functions, something like this:

>>void first(long index)
>>{
>>  long count=-1;
>>  do
>>  {
>>    /* do stuff */
>>    count++;
>>  }
>>  while (count<index);
>>}

>>void second()
>>{
>>  unsigned long index;
>>  /* do stuff */
>>  first(index);
>>}

>>Then I got paranoid and changed "index" in second() to long. Was it
>>necessary, or was I overly paranoid?

>Well, in practice it would only make a difference if 1) the default type
>for your platform is signed;

What else would it be?  Isn't "signed long" equivalent to "long"?

(In other words, are you confusing it with char?)



Thu, 30 Jan 2003 03:00:00 GMT  
 long vs unsigned long


Quote:

>What else would it be?  Isn't "signed long" equivalent to "long"?

>(In other words, are you confusing it with char?)

For some reason I had the idea that the signed-ness of integral types
was implementation-dependent. Ie, you're quite right; I was confusing it
with char. Apologies.

--
Hong Ooi                    | Centre for Maths and its Applications/

Ph: (02) 6267 4140          | Australian National University
                            | ACT 0200 Australia



Thu, 30 Jan 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. UINT, LONG vs. unsigned, long

2. char size (was long long long long long int)

3. format specifier for unsigned long long

4. printing unsigned long long problem

5. unsigned long long int

6. long long long long integers

7. unsigned long to long without lossage (Help)

8. long long vs. __int64

9. (unsigned int)(unsigned long)(void *)p

10. How to convert unsigned long to unsigned char?

11. Converting Unsigned Short to Unsigned long

12. short/long/long long Formatting questions

 

 
Powered by phpBB® Forum Software