C Library Function toupper() and tolower()


In this tutorial, you will learn about C library function toupper() and tolower() which is a character handling function used to check whether the argument supplied is a lowercase and uppercase respectively or not.

C Library Function toupper() and tolower()

We should include ctype.h header to use tolower( ) and toupper( ) function.

#include <ctype.h>

tolower( )


tolower converts an uppercase letter (A-Z) to a lowercase letter (a-z).

Function prototype of tolower( )

int tolower( int ch );

This function returns ch as a lowercase letter if ch is a uppercase letter. If the argument is the lowercase letter, it returns the argument unchanged.

toupper( )


toupper converts a lowercase letter (a-z) to an uppercase letter (A-Z).

Function prototype of toupper( )

int toupper( int ch );

This function returns ch as a uppercase letter if ch is a lowercase letter. If the argument is a uppercase letter, it returns the argument unchanged.

C Library Function toupper() and tolower(): Example

//Use of C library function toupper() and tolower()
#include <stdio.h>
#include <ctype.h>

int main()
{
  printf("Demonstration of tolower:\n");
  printf("\nA converted to lowercase: %c", tolower('A'));
  printf("\n@ converted to lowercase: %c", tolower('@'));
  printf("\n3 converted to lowercase: %c", tolower('3'));

  printf("\n\nDemonstration of toupper:\n");
  printf("\na converted to uppercase: %c", toupper('a'));
  printf("\n@ converted to uppercase: %c", toupper('@'));
  printf("\n3 converted to uppercase: %c", toupper('3'));

 return 0;

}

Output

Demonstration of tolower:

A converted to lowercase: a
@ converted to lowercase: @
3 converted to lowercase: 3

Demonstration of toupper:

a converted to uppercase: A
@ converted to uppercase: @
3 converted to uppercase: 3

Explanation

In the above program, a is converted to uppercase A with the help of toupper. However, @ and 3 remained unchanged in both functions as we discussed earlier.

Similarly, A is converted to lowercase a with the help of tolower.