C toupper() Function Tutorial

In this section, we will learn what the toupper() function is and how to use it in C.

C toupper() Function

The toupper() function takes an alphabet character as its argument and returns the uppercase version of that character.

For example:

The `f` character to `F` or `g` to `G` etc.

Note: the prototype of the function exists in the `ctype.h` header file and so we need to include the header file in order to work with the function.

toupper() Function Syntax

Here’s the prototype of the function:

int toupper( int arg );

toupper() Function Parameters

This function takes one argument and that is the character we want to get its uppercase version.

Note: the argument is of type integer, but because characters are also treated as integer behind the scene, it is OK to put a character as the argument of the function.

toupper() Function Return Value

The return value of this function is the uppercase character of the function’s argument.

Example: using toupper() function in C

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

void uppercase(char * pointer);

int main() {

    char stringOne[10]  = "HELLO";
    char stringTwo[10] = "hello";

    int result = strcmp(stringOne, stringTwo);
    printf("The result of the comparison before converting the characters into uppercase: %d\n", result);

    uppercase(stringOne);
    uppercase(stringTwo);

    result = strcmp(stringOne, stringTwo);

    printf("The result of the comparison after converting the characters into uppercase: %d\n", result);

    return  0;
}

void uppercase(char *pointer){

    for (; *pointer != '\0';){
        *pointer = toupper(*pointer);
        pointer++;

    }
}

Output:

The result of the comparison before converting the characters into uppercase: -1

The result of the comparison after converting the characters into uppercase: 0
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies