A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
tolower converts an uppercase letter to its lowercase equivalent. If the character is not an uppercase letter, it returns it unchanged. The argument must be representable as unsigned char or equal to EOF. Passing a negative char value is undefined behavior, the same rule that applies to every ctype function. The return value is an int representing the converted character. This is the function you use for case-insensitive comparisons, normalization, and input sanitization.
#include <stdio.h>#include <ctype.h>#include <string.h>void normalize_lowercase(char *s) {for (; *s; s++) {*s = (char)tolower((unsigned char)*s);}}int case_insensitive_equal(const char *a, const char *b) {while (*a && *b) {if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) {return 0;}a++;b++;}return *a == *b;}int main(void) {char msg[] = "Hello WORLD 123!";printf("Before: %s\n", msg);normalize_lowercase(msg);printf("After: %s\n", msg);printf("\nCompare \"Admin\" vs \"admin\": %d\n",case_insensitive_equal("Admin", "admin"));printf("Compare \"Admin\" vs \"root\": %d\n",case_insensitive_equal("Admin", "root"));return 0;}
The normalize function converts in place. The comparison function converts both characters on the fly without modifying either string. Both cast to unsigned char before calling tolower. Both handle non-letter characters correctly: digits, punctuation, and spaces pass through unchanged. The cast back to char in the normalize function is safe because tolower returns a value representable as unsigned char.
tolower is the workhorse of case normalization. Use it for case-insensitive lookups, command parsing, and input cleanup. Always cast to unsigned char. Always. Next: isalpha, for testing whether a character is a letter.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.