YouTip LogoYouTip

C Function Islower

## C Library Function - islower() The `islower()` function is a built-in C library function used to check whether a passed character is a lowercase letter ('a' to 'z'). This function is declared in the `` header file. --- ## Syntax ```c int islower(int c); ``` ### Parameters * **`c`**: This is the character to be checked. Although the parameter is of type `int`, it is passed as an unsigned char cast to an int, or `EOF` (End-Of-File). ### Return Value * **Non-zero value (True)**: If the passed character `c` is a lowercase letter. * **Zero (0) (False)**: If the passed character `c` is not a lowercase letter. --- ## How it Works (Locale Dependency) In the default `"C"` locale, `islower()` only returns true (a non-zero value) for lowercase ASCII characters: * `'a'` through `'z'` (ASCII values 97 to 122). In other locales, different characters may be considered lowercase, but they will always be characters for which neither `iscntrl()`, `isdigit()`, `ispunct()`, nor `isspace()` is true. --- ## Code Examples ### Example 1: Basic Usage The following program demonstrates how to check individual characters using `islower()`. ```c #include #include int main() { int var1 = 'Q'; int var2 = 'q'; int var3 = '3'; if ( islower(var1) ) { printf("var1 = |%c| is a lowercase letter.\n", var1); } else { printf("var1 = |%c| is not a lowercase letter.\n", var1); } if ( islower(var2) ) { printf("var2 = |%c| is a lowercase letter.\n", var2); } else { printf("var2 = |%c| is not a lowercase letter.\n", var2); } if ( islower(var3) ) { printf("var3 = |%c| is a lowercase letter.\n", var3); } else { printf("var3 = |%c| is not a lowercase letter.\n", var3); } return 0; } ``` #### Output ```text var1 = |Q| is not a lowercase letter. var2 = |q| is a lowercase letter. var3 = |3| is not a lowercase letter. ``` --- ### Example 2: Counting Lowercase Characters in a String This practical example shows how to use `islower()` to count the number of lowercase letters in a given string. ```c #include #include int main() { int i = 0; int lowercase_count = 0; char str[] = "Welcome to YouTip C Tutorials!"; while (str != '\0') { if (islower(str)) { lowercase_count++; } i++; } printf("The string contains %d lowercase letters.\n", lowercase_count); return 0; } ``` #### Output ```text The string contains 20 lowercase letters. ``` --- ## Important Considerations 1. **Undefined Behavior**: The behavior of `islower()` is undefined if the argument `c` is not representable as an `unsigned char` or is not equal to `EOF`. 2. **Safe Casting**: To safely use `islower()` with standard `char` types (especially when dealing with signed characters that might have negative values), it is best practice to cast the argument to `unsigned char`: ```c char ch = 'a'; islower((unsigned char)ch); ``` 3. **Related Functions**: * `isupper()`: Checks if a character is uppercase. * `tolower()`: Converts a character to lowercase. * `isalpha()`: Checks if a character is alphabetic (either uppercase or lowercase).
← C Function IsprintC Function Isgraph β†’