Thread: about: FAQ > How do I... (Level 2) > How can I convert a char/string to upper or lo

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    1

    about: FAQ > How do I... (Level 2) > How can I convert a char/string to upper or lo

    I used google with string and toupper, and this was displayed as first hit :-).

    I noticed that the C versions of the code are not correct tough, so in order to get it improved for the future I wanted to add some notes.
    The main problem I saw is that the ANSI-C tolower is used with a char.
    Ansi-C's tolower however only expects unsigned char's or EOF, the actual argument is an int.
    For at least some conforming implementations I know, tolower uses its argument as index into an array, and for negative char's (if char happens to be signed, e.g. Win32), this results in a out of bound memory access.
    If you want to use a char, you always have to cast it to unsigned char (as ugly as it may look).

    char c= tolower((unsigned char)inputCh);

    (I also wonder is this (int(*)(int)) cast is really reasonable for the C++ version. I did never notice that casts would resolve ambiguity, but not sure, and of course of topic in a C thread)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by c99 draft
    7.4.2 Character case mapping functions
    7.4.2.1 The tolower function
    Synopsis
    1 #include <ctype.h>
    int tolower(int c);
    Description
    2 The tolower function converts an uppercase letter to a corresponding lowercase letter.
    Returns
    3 If the argument is a character for which isupper is true and there are one or more
    corresponding characters, as specified by the current locale, for which islower is true,
    the tolower function returns one of the corresponding characters (always the same one
    for any given locale); otherwise, the argument is returned unchanged.
    Where's all this unsigned stuff coming from ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Where's all this unsigned stuff coming from ?
    Earlier in the section:
    Quote Originally Posted by Final C99 hardcopy
    In all cases, the argument is an int, the value of which shall be representable as an unsigned char or shal equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.
    The cast to unsigned char is the least of all evils when the argument could possibly be a character not returned by an fgetc-like source, which guarantees the correct representation.

    >If you want to use a char, you always have to cast it to unsigned char (as ugly as it may look).
    Not always, only when there's a chance that the argument might not meet the requirements. This applies if you pass an int as well, because any value other than the strict values representable by unsigned char or EOF results in undefined behavior.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert a string to upper case?
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 07:49 PM