toupper in C Programming

The C toupper function is one of the Standard Library Functions available in the C language, used to convert the given character into an Uppercase character. The syntax of the C toupper function is shown below.

The below function accepts the single character as the parameter and converts the given character to uppercase using the toupper in C Programming language.

toupper(char)

toupper in C Programming Example

The toupper function is used to convert the given character to uppercase. This C program allows the user to enter any character and convert that character to uppercase using the toupper function.

#include <stdio.h>
#include <ctype.h>

int main()
{
   char ch;
   printf("Please Enter Any Valid Char: \n");
   scanf("%c", &ch);

   printf("\n Equivalent Upper Case = %c", toupper(ch));         

}
Please Enter Any Valid Char: 
g

 Equivalent  Upper Case = G

First, we declared a character variable called ch.

char ch;

The following C Programming statement will ask the user to enter any character. And then, we are using the scanf to assign the user entered character to the ch variable.

printf("Please Enter Any Valid Char: \n");
scanf("%c", &ch);

Next, we used the C toupper function directly inside the printf statements to print the output. The below statement will convert the character in the ch variable to uppercase.

printf("\n Equivalent Upper Case = %c", toupper(ch));

The above code will definitely convert the given character to uppercase, but what if we enter the numeric value?

Please Enter Any Valid Char: 
6

 Equivalent Upper Case = 6

As you can see, it is not throwing any error, which might not be good in real time.

toupper in C Programming Example 2

In this example, we will show you a better approach to the program mentioned above.

#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;
    printf("Please Enter Any Valid Character: \n");
    scanf("%c", &ch);

    if(isalpha(ch))
    {
      printf("\n We Converted Your character to Upper Case = %c", toupper(ch));         
    }
    else
    {
      printf("\n Please Enter a Valid character"); 
    }
}
toupper in C Programming 3

In this C toupper function code snippet, we added the If Statement to check whether the character is between ‘A’ and ‘Z’ using the isalpha function. If the condition is True, it will convert the given character to Uppercase using the below statement.

printf("\n We Converted Your character to Upper Case = %c", toupper(ch));

If the above condition is FALSE, then the given character is not Alphabet. So, it will print the below statement.

printf("\n Please Enter a Valid character");

Let me enter the numeric value as input.

Please Enter Any Valid Character: 
5

 Please Enter a Valid character

TIP: Please refer to C Program to Convert to an Uppercase article. This is useful to understand how to convert the character to lowercase without using the toupper function.