Table of contents
1.
Introduction
2.
What is <ctype.h>?
3.
Example
4.
Frequently Asked Questions
4.1.
Are the functions in <ctype.h> case-sensitive?
4.2.
Can the <ctype.h> functions be used with non-ASCII characters?
4.3.
Are the <ctype.h> functions affected by the current locale settings?
5.
Conclusion
Last Updated: Nov 30, 2024
Easy

ctype.h in C

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

ctype.h is a header file in the C standard library that provides functions to classify and transform individual characters. These functions can determine whether characters are alphabetic, numeric, or even whitespace, among other categories. With the help of these functions, we can manipulate characters and provide useful information about them. This is very important for parsing text, validating input, and processing strings in numerous C programs. 

ctype.h in C

In this article, we will discuss the functions provided by ctype.h and understand how to use them with the help of examples.

What is <ctype.h>?

<ctype.h> is a header file in the C programming language that provides functions for testing & mapping characters. It allows you to perform various operations on characters, such as checking if a character is an alphabet, digit, lowercase, uppercase, punctuation mark, etc. These functions take a single character as an argument & return a non-zero value (true) if the character satisfies the condition, or zero (false) otherwise.

Some commonly used functions from the <ctype.h> header file are:

  • isalpha(c): Checks if the character 'c' is an alphabet (a-z or A-Z).
     
  • isdigit(c): Checks if the character 'c' is a digit (0-9).
     
  • islower(c): Checks if the character 'c' is a lowercase alphabet (a-z).
     
  • isupper(c): Checks if the character 'c' is an uppercase alphabet (A-Z).
     
  • isalnum(c): Checks if the character 'c' is an alphanumeric character (a-z, A-Z, or 0-9).
     
  • isspace(c): Checks if the character 'c' is a white-space character (space, tab, newline, etc.).
     
  • ispunct(c): Checks if the character 'c' is a punctuation mark.
     
  • tolower(c): Converts the character 'c' to lowercase if it is an uppercase alphabet.
     
  • toupper(c): Converts the character 'c' to uppercase if it is a lowercase alphabet.

Example

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


int main() {
    char c = 'A';
    
    if (isalpha(c)) {
        printf("%c is an alphabet.\n", c);
    }
    
    if (isdigit(c)) {
        printf("%c is a digit.\n", c);
    }
    
    if (islower(c)) {
        printf("%c is lowercase.\n", c);
    }
    
    if (isupper(c)) {
        printf("%c is uppercase.\n", c);
    }
    
    char lowercaseChar = tolower(c);
    printf("Lowercase of %c is %c.\n", c, lowercaseChar);
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

A is an alphabet.
A is uppercase.
Lowercase of A is a.


In this example, we include the <ctype.h> header file & use various functions to check the properties of the character 'A'. We use `isalpha()` to check if it's an alphabet, `isdigit()` to check if it's a digit, `islower()` & `isupper()` to check its case, and `tolower()` to convert it to lowercase.

Now, we will discuss the functions which we discussed above in detail like how they are different and their return value: 

FunctionDescriptionReturn Value
isalpha(c)Checks if the character c is an alphabet (a-z or A-Z)Non-zero if true, zero if false
isdigit(c)Checks if the character c is a digit (0-9)Non-zero if true, zero if false
islower(c)Checks if the character c is a lowercase alphabet (a-z)Non-zero if true, zero if false
isupper(c)Checks if the character c is an uppercase alphabet (A-Z)Non-zero if true, zero if false
isalnum(c)Checks if the character c is alphanumeric (a-z, A-Z, 0-9)Non-zero if true, zero if false
isspace(c)Checks if the character c is a white-space characterNon-zero if true, zero if false
ispunct(c)Checks if the character c is a punctuation markNon-zero if true, zero if false
tolower(c)Converts the character c to lowercaseLowercase character
toupper(c)Converts the character c to uppercaseUppercase character


Let’s look at a few more examples using these functions: 

1: If you want to check if a user input is a valid digit, you can use the isdigit() function:

char input = getchar();

if (isdigit(input)) {
    printf("Valid digit entered.\n");
} else {
    printf("Invalid input. Please enter a digit.\n");
}

 

2: Similarly, you can use tolower() & toupper() functions to convert characters between lowercase & uppercase:

char c = 'H';
char lowercaseChar = tolower(c);
char uppercaseChar = toupper(lowercaseChar);

printf("Original: %c, Lowercase: %c, Uppercase: %c\n", c, lowercaseChar, uppercaseChar);


Output:

Original: H, Lowercase: h, Uppercase: H

Frequently Asked Questions

Are the functions in <ctype.h> case-sensitive?

Yes, the functions in <ctype.h> are case-sensitive. For example, 'a' is considered lowercase, while 'A' is considered uppercase.

Can the <ctype.h> functions be used with non-ASCII characters?

The behavior of <ctype.h> functions with non-ASCII characters is implementation-defined & may vary across different platforms.

Are the <ctype.h> functions affected by the current locale settings?

Yes, some of the <ctype.h> functions, such as isalpha() & ispunct(), are affected by the current locale settings.

Conclusion

In this article, we discussed the <ctype.h> header file in C and its various functions for testing & manipulating characters. We learned about commonly used functions like isalpha(), isdigit(), tolower(), & toupper(), along with their descriptions, return values with few examples also.

You can also check out our other blogs on Code360.

Live masterclass