C Reference ยท ctype.h, math.h and others
C toupper() and tolower() functions
Reference for C toupper() and tolower() from ctype.h: convert single characters to upper or lower case, convert whole strings and avoid common mistakes.
toupper() returns the uppercase version of a character and tolower() the lowercase version. If the character is not a letter, or has no counterpart in the other case, it is returned unchanged. They convert one character at a time and return an int; the C library has no standard function that converts a whole string, so you write a loop.
Typical uses are case-insensitive comparison, normalizing commands typed by a user and capitalizing the first letter of a word.
#include <ctype.h> int toupper(int c); int tolower(int c);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int toupper(int c); int tolower(int c);
| Name | Description |
|---|---|
c |
A value representable as unsigned char, or EOF. |
Returns: the converted character as an int (convertible to unsigned char), or c itself if no conversion applies. EOF is returned unchanged.
Errors / undefined behavior: a c outside the range of unsigned char (other than EOF), such as a negative char, is undefined behavior.
Usage notes
- Assign back. The functions do not modify anything;
toupper(ch);alone does nothing. Writech = (char)toupper((unsigned char)ch);. - Cast on the way in and out. Cast the argument to
unsigned charso that high-bit characters cannot cause undefined behavior, and cast the result back tocharwhen storing it. - Characters printed with
%care fine, asprintf("%c", toupper(c))converts theintback tounsigned char. - ASCII only in practice. In the
"C"locale onlya-zandA-Zchange. Other locales may map single-byte letters such asรฉ/ร, but multi-byte UTF-8 text is not handled byte by byte. Languages with special rules (GermanรbecomesSSin uppercase, Turkish dotted and dotlessi) cannot be expressed in a one-character-to-one-character function. Usetowupperfor wide characters or a Unicode library for correct results. - Case-insensitive search. Compare with
toloweron both sides, as in the example. POSIX systems offerstrcasecmpin<strings.h>, which is not part of ISO C. - Pairs are not always symmetric in other locales, so
tolower(toupper(c)) == cis not guaranteed in general. - Avoid side effects in the argument (
toupper(*p++)), because the function might be implemented as a macro. - Arithmetic tricks such as
c - 'a' + 'A'assume contiguous letters, which ASCII has and EBCDIC does not; the library functions are the portable choice. - Standard version. Both are in C89.