C tolower() Function Tutorial

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

C tolower() Function

The tolower() function takes an alphabet character as its argument and returns its lowercase version.

For example:

The `F` character to `f` or `G` character 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.

tolower() Function Syntax

Here’s the prototype of the function:

int tolower( int arg );

tolower() Function Parameters

This function takes one argument and that is the character that we want to get its lowercase 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.

tolower() Function Return Value

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

Example: lower C via tolower() function

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

void lowercase(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 lowercase: %d\n", result);

    lowercase(stringOne);
    lowercase(stringTwo);

    result = strcmp(stringOne, stringTwo);

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

    return  0;
}

void lowercase(char *pointer){

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

    }
}

Output:

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

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

Top Technologies