Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <ctype.h>

int toupper(int c);
int tolower(int c);

Try it

C Converting a string in place

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.

C Case-insensitive comparison

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

C
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. Write ch = (char)toupper((unsigned char)ch);.
  • Cast on the way in and out. Cast the argument to unsigned char so that high-bit characters cannot cause undefined behavior, and cast the result back to char when storing it.
  • Characters printed with %c are fine, as printf("%c", toupper(c)) converts the int back to unsigned char.
  • ASCII only in practice. In the "C" locale only a-z and A-Z change. 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 รŸ becomes SS in uppercase, Turkish dotted and dotless i) cannot be expressed in a one-character-to-one-character function. Use towupper for wide characters or a Unicode library for correct results.
  • Case-insensitive search. Compare with tolower on both sides, as in the example. POSIX systems offer strcasecmp in <strings.h>, which is not part of ISO C.
  • Pairs are not always symmetric in other locales, so tolower(toupper(c)) == c is 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.