toupper() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The toupper in C function defined in <ctype.h> header file is used to convert lowercase alphabets to uppercase alphabets. It does not change uppercase letters and special characters or digits. If we want to convert all alphabets of a string from lowercase to uppercase, we need to traverse over the characters of the string one by one and apply toupper function to each character.

a ----->A
b ----->B
.
.
z ----->Z

What is toupper function in C

In C, the toupper function is a standard library function that is used to convert a lowercase character to its corresponding uppercase character. It is part of the <ctype.h> library and is commonly used for text manipulation and character conversions. Here's the basic syntax of the toupper function:

The toupper function takes a character as input and returns the uppercase version of that character. If the input character is not a lowercase letter, it is returned unchanged. It is important to note that this function works only on individual characters, and it does not modify the original string or character.

How to use toupper function in C?

To use the toupper function in C, you need to include the <ctype.h> header, and then you can apply it to individual characters. Here's how to use toupper:

  1. Include the <ctype.h> header at the beginning of your C program.
  2. Use the toupper function to convert a character to uppercase.
  3. Store the result in a variable or use it directly.

Here's a simple example:

In this example:

  • We include the <ctype.h> header to access the toupper function.
  • We define a character lowercase with the value 'a'.
  • We use the toupper function to convert the character 'a' to its uppercase equivalent 'A' and store it in the variable uppercase.
  • We print both the original character and the uppercase version to the console.

When you run the program, you will see the following output:

The toupper function is a simple way to convert lowercase characters to uppercase in C. You can apply it to individual characters or incorporate it into more complex string manipulations as needed.

Syntax of toupper function in C

The syntax of toupper in C is:

Here c is the character to be converted from lowercase to uppercase. The ASCII value of the character is passed to the toupper function.

Parameters of toupper function in C

The toupper function takes just a single parameter, the character to be converted from lowercase to uppercase. The ASCII value of the character is passed to the toupper function.

Here c is the character to be converted from lowercase to uppercase.

Return Values of toupper function in C

The return value of the toupperfunction is the corresponding uppercase letter of the lowercase letter that is to be transformed.

Exceptions of toupper function in C

Following are the exceptions of toupper-

  1. If an uppercase letter is passed to the toupper function, then it itself is returned.
  2. If a special symbol or a digit is passed, then the toupper function will return the same character.

Example

The following code depicts an example of toupper in C.

Output:

Explanation:

We have the string "Example @ toupper #123". We traverse the string using a while loop and apply toupper for every character. The lowercase characters get converted into uppercase characters, while the uppercase characters, digits, and special symbols remain the same. The final string is, therefore "EXAMPLE @TOUPPER #123".

More Examples

Let us now see more examples of how toupper works with different groups of characters.

Example 1: Uppercase Alphabets

The toupper function doesn't change the uppercase alphabets. Here is an example where we use toupper for uppercase alphabets.

Output:

Explanation: As the input string contains entirely uppercase letters, the output string remains unchanged.

Example 2: Lowercase alphabets

The toupper function changes the lowercase alphabets to uppercase alphabets. Here is an example where we use toupper for lowercase alphabets.

Output:

Explanation:

As the input string contains lowercase characters, the output string contains the converted uppercase of all the characters.

Example 3 : Special characters / Numbers

The toupper function doesn't change special characters or numbers. Here is an example: toupper for digits and special numbers.

Output:

Explanation:

The' output' string remains unchanged as the input string contains special characters and numbers.

Conclusion

  • toupper is a library function in C.
  • toupper converts lowercase alphabets to uppercase alphabets.
  • toupper does not change uppercase alphabets, digits, and special characters.

See Also

Here are other C functions that are similar to the toupper function: