Skip to content
  • Morten Welinder's avatar
    THE WORLD'S BEST KEPT C SECRETS, PART 12: · e2df9a9e
    Morten Welinder authored
    ----------------------------------------
    
    The functions isalpha, isdigit, ..., isspace, tolower, and toupper
    are ONLY defined on EOF (typically -1) and unsigned characters.
    Read the manual pages from various OSs -- it's in there.
    
    It is wrong to call these with signed characters as argument; the
    type "char" can be, and often is, signed.  Please don't write code
    like
    
         void frob (char *s)
         {
           while (isspace (*s))       /* WRONG! */
             s++;
           /* ... */
         }
    
    Instead write something like
    
         void frob (char *s)
         {
           while (isspace ((unsigned char)*s))   /* Right */
             s++;
           /* ... */
         }
    
    Note: the above is true even if you don't like it.  Some people have
    gotten terrible upset and defensive when they have had this problem
    pointed out to them.  That does not help.
    
    FYI, all this makes a serious difference on Solaris, HP-UX and maybe
    other OSs.
    e2df9a9e