C tolower() function is part of the C standard Library used to convert the uppercase alphabet to the lowercase alphabet. It does not affect characters other than uppercase characters.
Example
#include <stdio.h>
#include <ctype.h>
int main() {
// Converting 'A' to 'a'
printf("%c", tolower('A'));
return 0;
}
Output
a
Syntax of tolower()
The tolower() function is defined in the <ctype.h> header file.
int tolower(int c);
where,
- c: character that is to be converted. It is generally passed as integer that can represent an unsigned char.
Return Value
- If the passed character is an uppercase character then it returns the ASCII value corresponding to the lowercase of the character passed as the argument.
- If the passed character is already a lowercase or not a letter then it returns the same character.
Examples of tolower() Functions
The below examples demonstrate how to use the tolower() function in C:
Example 1
The below C program demonstrates the tolower() function.
#include <ctype.h>
#include <stdio.h>
int main()
{
char ch = 'M';
printf("Original Character: %c\n", ch);
// convert ch to lowercase
ch=tolower(ch);
printf("After using tolower: %c", ch);
return 0;
}
Output
Original Character: M After using tolower: m
Example 2
The below code converts all uppercase letters in the string to their lowercase equivalents, while leaving other characters unchanged.
#include <ctype.h>
#include <stdio.h>
int main()
{
char s[] = "Code_in_C_@0123";
printf("Original String: %s\n", s);
// This will just convert
// uppercase letters in string
// to lowercase. Other characters
// will remain unaffected.
for (int i = 0; i < strlen(s); i++) {
s[i] = tolower(s[i]);
}
printf("Converted to lowercase: %s", s);
return 0;
}
Output
code_in_c_@0123