Rewrite C Function: isupper

Last Updated : 10 July, 2022 · 2 min read

articles banner image

The goal of this tutorial is to closely investigate the isupper function in the C programming language. This tutorial is divided into four relevant parts:

Structure of isupper:

Declaration: int isupper(int c);

Return: non-zero value if int c is an uppercase alphabetic value. Otherwise, 0.

Parameter list:
(int c) - the character that is checked to determine the return value.

Header file: <ctype.h>.

1. So, what is the isupper function in C?

This function checks if the passed char value is an uppercase alphabetic value or not, and returns an appropriate value of type int indicating the result.

If the argument is an uppercase alphabetic value, the isupper function returns a non-zero value.

If the argument is not a uppercase alphabetic value, the isupper function returns a zero.

Function isupper is defined in <ctype.h> library. To use this function, we need to include <ctype.h> header file into our C program (e.g. #include <ctype.h>).

Example 1: Usage of the isupper function in C.

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

int main( void )
{
    
printf ( "1. %d\n", isupper ( 'a' ));
    
printf ( "2. %d", isupper ( 'A' ));
    
return ( 0 );
}

Possible output:

1. 0
2. 1

2. How to rewrite the isupper function in C?

Consider the code example below for a moment:

Example 2: rewritten isupper function in C.

int our_isupper ( int c )
{
    
if ( c >= 65 && c <= 90 )
        
return ( 1 );
    
return ( 0 );
}

The our_isupper function above takes in one parameter. Since this parameter c is of int type, arguments of char type (when sent as arguments to this function) get implicitly typecasted to an according ASCII value.

Then it is checked if the received value of c is in the range of [65,90] (On line 3). If this condition is true, it means that the value is in the uppercase alphabet and 1 is returned. Otherwise 0 is returned, which is indicative that the value c is not in the uppercase alphabet.

3. Testing the rewritten isupper function

In the code example below, we have set up a simple test that checks if our_isupper function returns the same values as the isupper function from the standard C library.

Example 3: Test of the rewritten isupper function in C.

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

int our_isupper ( int c )
{
    
if ( c >= 65 && c <= 90 )
        
return ( 1 );
    
return ( 0 );
}
int main( void )
{
    
printf ( "1. our_isupper: %d, isupper: %d\n" , our_isupper ( 'a' ), isupper ( 'a' ));
    
printf ( "2. our_isupper: %d, isupper: %d\n" , our_isupper ( 'A' ), isupper ( 'A' ));
    
return ( 0 );
}

Possible output:

1. our_isupper: 0, isupper: 0
2. our_isupper: 1, isupper: 1

As you can see, the rewritten isupper function returns in this case exactly the same results as the C library function.

4. Quiz of the reader's knowledge

Quiz : Rewrite C Function (isupper)

1What is the return value of isupper('u')?

2In which library is isupper defined?

2What is the return value of isupper(66)?

quiz completed!

Congrats!

Feel free to share your achievement below!