Rewrite C Function: toupper

Last Updated : 10 July, 2022 · 2 min read

The standard C library offers a number of functions that allow users to modify character values. The toupper function is one of such functions, which we are going to discuss here. In this article, you can expect to:

Structure of toupper:

Declaration: int toupper(int c);

Return: unmodified integer value of parameter c or a difference of the received parameter and 32 (c - 32).

Parameter list:
(int c) - an integer value to be modified, if it represents a lowercase character.

Header file: <ctype.h>.

1. What is toupper function?

The toupper function in C programming language is helpful when you want to convert a lowercase character to an uppercase one. If you pass a value that does not have a lowercase character representation, then the toupper function returns the value of the received parameter.

Hence, the value returned from the toupper function can only be less by 32 or equal to the received parameter. Below, you can find an example of the usage of this standard function (example 1).

Example 1: Printing uppercase alphabet using toupper function.

# include <stdio.h>
# include <ctype.h>
int main ()
printf ( "Now you know the " );
for ( int i = 'a' ; i <= 'z' ; i++)
printf ( "%c" , toupper(i));
return 0;

Possible output:


2. And how to rewrite the toupper function in C?

To rewrite the toupper function, you should consider the following: the return value is the difference of the received parameter and 32 only if the received parameter can be directly converted to a lowercase character. Otherwise, the return value is the same as the received parameter. Below you can find, how this looks in C programming language (example 2).

Example 2: Rewritten toupper function in C.

int our_toupper ( int c)
if (c >= 'a' && c <= 'z' )
return (c - 32);
return (c);

In the conditional statement (on line 3), we are checking if the received parameter is within a and z character values (from 97 to 122 in ASCII). In which case, we subtract 32 and return the difference (on line 4). If the received parameter is not within this range and is not a lowercase character, then an unchanged value is returned (on line 5).

3. Quiz of the toupper function

Quiz : Rewritten toupper function in C

1What does the toupper function NOT do?

2Which of the following ranges contain all the uppercase alphabet characters?

3What is the return value of toupper(102)?

quiz completed!


Feel free to share your achievement below!