home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 241,369 of 243,242   
   David Brown to Scott Lurndal   
   Re: signed vs unsigned and gcc -Wsign-co   
   21 Oct 25 09:13:38   
   
   From: david.brown@hesbynett.no   
      
   On 20/10/2025 20:01, Scott Lurndal wrote:   
   > Michael S  writes:   
   >> On Mon, 20 Oct 2025 17:03:58 +0200   
   >> pozz  wrote:   
   >>   
   >>> After many years programming in C language, I'm always unsure if it   
   >>> is safer to use signed int or unsigned int.   
   >>>   
   >>> Of course there are situations where signed or unsigned is clearly   
   >>> better. For example, if the values could assume negative values,   
   >>> signed int is the only solution. If you are manipulating single bits   
   >>> (&, |, ^, <<, >>), unsigned ints are your friends.   
   >>>   
   >>> What about other situations? For example, what do you use for the "i"   
   >>> loop variable?   
   >>>   
   >>   
   >> I'd just point out that small negative numbers are FAR more common than   
   >> numbers in range [2**31..2**32-1].   
   >> Now, make your own conclusion.   
   >   
   > One might also point out that negative loop indicies are rare, and   
   > thus ones conclusion may be that generally speaking loop indexes should   
   > be unsigned.   
   >   
      
   Loop indicies greater than 2 ^ 31 are equally rare.  (Loops of between 2   
   ^ 15 and 2 ^ 16 - 1 on 8-bit and 16-bit targets are less unrealistic.)   
      
   Loops where you actually want the index counter to wrap are very rare,   
   except perhaps when your loop is shifting the index each count (and then   
   you are firmly in unsigned type territory).   
      
   So in general, you are dealing with numbers that will fit comfortably   
   within the ranges of both "int" and "unsigned int".  If there is no   
   other deciding factor, pick the one with the fewest unnecessary   
   additional specifications, since that gives the compiler the most   
   flexibility - "int".   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca