What is <ctype.h>?
The <ctype.h> header file in C contains a set of functions that perform tests and conversions on individual characters. These functions work with characters represented by int values (usually ASCII values).
Before using these functions, make sure to include the header in your program:
#include <ctype.h>
Character Classification Functions
These functions help determine the type or category of a character:
Function | Description |
---|---|
isalnum(int c) | Checks if the character is alphanumeric (letter or digit). |
isalpha(int c) | Checks if the character is alphabetic. |
isdigit(int c) | Checks if the character is a digit. |
islower(int c) | Checks if the character is lowercase. |
isupper(int c) | Checks if the character is uppercase. |
isspace(int c) | Checks if the character is a whitespace character (space, tab, newline, etc.) |
ispunct(int c) | Checks if the character is a punctuation character. |
isxdigit(int c) | Checks if the character is a hexadecimal digit. |
isprint(int c) | Checks if the character is printable, including space. |
isgraph(int c) | Checks if the character has a graphical representation, excluding space. |
iscntrl(int c) | Checks if the character is a control character. |
isascii(int c) | Checks if the character is an ASCII character (0 to 127). |
isalpha()
Checks whether the given character is an alphabet (A-Z or a-z).
Syntax:
int isalpha(int ch);
Example:
#include <stdio.h> #include <ctype.h> int main() { char quiz = 'S'; if (isalpha(quiz)) { printf("%c is an alphabet character.\n", quiz); } else { printf("%c is not an alphabet character.\n", quiz); } return 0; }
Output:
S is an alphabet character.
isdigit()
Checks if the character is a digit (0-9).
Syntax:
int isdigit(int ch);
Example:
#include <stdio.h> #include <ctype.h> int main() { char cert = '9'; if (isdigit(cert)) { printf("%c is a digit.\n", cert); } else { printf("%c is not a digit.\n", cert); } return 0; }
Output:
9 is a digit.
islower() and isupper()
islower() checks if character is lowercase and isupper() checks if character is uppercase.
Syntax:
int islower(int ch); int isupper(int ch);
Example:
#include <stdio.h> #include <ctype.h> int main() { char mcqs = 'Q'; if (isupper(mcqs)) { printf("%c is uppercase.\n", mcqs); } mcqs = 'q'; if (islower(mcqs)) { printf("%c is lowercase.\n", mcqs); } return 0; }
Output:
Q is uppercase.
q is lowercase.
isspace()
Checks if a character is a whitespace character (space, tab, newline).
Syntax:
int isspace(int ch);
Example:
#include <stdio.h> #include <ctype.h> int main() { char input = ' '; if (isspace(input)) { printf("Character is a whitespace.\n"); } else { printf("Character is not a whitespace.\n"); } return 0; }
Output:
Character is a whitespace.
isxdigit(int c)
Checks if the character c is a hexadecimal digit: ‘0’-‘9’, ‘A’-‘F’, ‘a’-‘f’.
Syntax:
int isxdigit(int c);
Example:
#include <stdio.h> #include <ctype.h> int main() { char mcqs = 'F'; if (isxdigit(mcqs)) { printf("%c is a valid hexadecimal digit.\n", mcqs); } else { printf("%c is not a hexadecimal digit.\n", mcqs); } return 0; }
Output:
F is a valid hexadecimal digit.
isascii(int c)
Checks if the character c is a valid ASCII character (0–127).
Note: isascii() is a POSIX function and not part of the standard C library; it’s available on many systems like Linux.
Syntax:
int isascii(int c);
Example:
#include <stdio.h> #include <ctype.h> int main() { int test = 120; // ASCII for 'x' if (isascii(test)) { printf("%c is an ASCII character.\n", test); } else { printf("Character is not in ASCII range.\n"); } return 0; }
Output:
x is an ASCII character.
Character Conversion Functions
These functions convert characters from one case to another:
- tolower(int c): Converts an uppercase letter to its lowercase equivalent. If the character is not an uppercase letter, it returns the character unchanged.
- toupper(int c): Converts a lowercase letter to its uppercase equivalent. If the character is not a lowercase letter, it returns the character unchanged.
Example:
#include <stdio.h> #include <ctype.h> int main() { char test = 's'; printf("Original: %c, Upper: %c\n", test, toupper(test)); test = 'F'; printf("Original: %c, Lower: %c\n", test, tolower(test)); return 0; }
Output:
Original: s, Upper: S Original: F, Lower: f
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice BCA MCQs
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for Computer Science Internship
- Apply for C Internship