isalpha() Function Example in C Programming

In this source code example, we will see how to use the isalpha() function in C programming with an example.

isalpha() Function Overview

The isalpha() function is present in <ctype.h> library in C, it checks if the provided character is an alphabetic letter, either uppercase (A-Z) or lowercase (a-z). 

Key Points:

- It necessitates the inclusion of the <ctype.h> header. 

- The function accepts an int (typically a character) as its argument. 

- It returns a non-zero value (true) if the character is alphabetic; otherwise, it gives back 0 (false).

Source Code Example

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

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    if (isalpha(ch))
        printf("%c is an alphabetic character.\n", ch);
    else
        printf("%c is not an alphabetic character.\n", ch);

    return 0;
}

Output

Enter a character: B
B is an alphabetic character.

Explanation

1. We first include the necessary header files: stdio.h for input/output functions and ctype.h for character functions.

2. Within the main() function, a character variable ch is initialized.

3. The user is then prompted to enter a character.

4. The isalpha() function examines the provided character to see if it's alphabetic.

5. Depending on the result, an appropriate message is printed to the console.


Comments