The C toupper function is one of the Standard Library Functions available in C language, used to convert the given character into Uppercase character. The syntax of the toupper in C Programming language is
The below function accepts the single character as the parameter, and convert the given character to uppercase using the toupper in C.
toupper(char)
toupper in C Programming Example
The toupper function 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.
//Example for toupper in C Programming #include <stdio.h> #include <ctype.h> int main() { char ch; printf("Please Enter Any Valid Character: \n"); scanf("%c", &ch); printf("\n We Converted Your character to Upper Case = %c", toupper(ch)); }
OUTPUT
ANALYSIS
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 ch variable
printf("Please Enter Any Valid Character: \n"); scanf("%c", &ch);
Next, we used the toupper function directly inside the printf statements to print the output. The below statement will convert the character in ch variable to uppercase
printf("\n We Converted Your character to Upper Case = %c", toupper(ch));
The above code will definitely convert the given character to uppercase, but what if we enter the numeric value
As you can see, it is not throwing any error, which might not be good at real-time.
toupper in C Programming Example 2
In this toupper example, we will show you a better approach to the program mentioned above.
//Example for toupper in C Programming #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"); } }
OUTPUT
ANALYSIS
In this toupper code snippet, we added the If Statement to check whether the character is between ‘A’ and ‘Z’ using isalpha function. If the condition is True, it will convert the given character to Uppercase using 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 an input
TIP: Please refer C Program to Convert Character to Uppercase article. This is useful to understand how to convert the character to lowercase without using the toupper function.