×

Character Class Tests in C

Introduction

The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple functions which are beneficial for not only testing characters but also mapping them.

In the <ctype.h>, each function that has the argument is an int. What this means is, that all the functions accept int as a parameter, whose value must be EOF (End of File) or representable as an unsigned char, and the return value is an int.

Note: If the argument c satisfies the described conditions, then the functions return non-zero (true) and in case it does not satisfy the conditions, the functions return zero(false).

Library Functions

S. No. Function Description
1 isalnum(c)It stands for is_alphanumeric() and it checks whether the passed character is alphanumeric or not.
2isalpha(c)It is an acronym to test a character as alphabetic and it checks whether the passed character is alphabetic.
3iscntrl(c)Short for control character and it checks whether the passed character is a control character.   (Control character)
4isdigit(c)This function is used to check whether the passed character is a decimal digit.   (decimal digit)
5isgraph(c)This function is used to check whether the passed character has graphical representation using locale.   (printing character except space)
6islower(c)This function is used to check whether the passed character is a lowercase letter.   (lower-case letter)
7isprint(c)This function is used to check whether the passed character is printable.   (printing character except space)
8ispunct(c)This function is used to check whether the passed character is a punctuation character.   (It prints characters with the exception of space or letter or digit)
9isspace(c)This function is used to check whether the passed character is white-space.   (space, form feed, newline, carriage return, tab, vertical tab)
10isupper(c)This function is used to check whether the passed character is an uppercase letter.   (upper-case letter)
11isxdigit(c)This function is used to check whether the passed character is a hexadecimal digit.   (hexadecimal digit)

 

 

In C programming, the seven bit ASCII character set consists of ;

  • The printing characters which lies in the range : 0 x 20 (‘  ‘) to 0 x 7E (‘ ~ ‘)
  • And, The control characters which lies in the range : 0 (NULL) to 0 x 1F(US), and 0x7F(DEL)

Note: There are a few special cases, and it consists of two functions that converts the case of letter (either uppercase or lowercase):

int tolower (int c)Converts c to lowercase
int toupper (int c)Converts c to uppercase

Here, one thing to keep a note of is,

  • If ‘c’ is a lowercase letter, toupper(c) returns the corresponding upper-case letter; otherwise, it returns c.
  • If ‘c’ is an uppercase letter, tolower(c) returns the corresponding lower-case letter; otherwise, it returns c.

Character Classes

S. No. Function Description
1Digitsset of whole numbers (range: 0 to 9)    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }.
2Hexadecimal digitsset of whole numbers + alphabets ranging from A to F or a to f (both cases)   { 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f }.
3LettersIt is a set or collection of letters from both cases. In simpler terms, it is a set of lowercase and uppercase letters.   The range of letters lies from: (a to z) and (A to Z)   { a b c d e f g h i j k l m n o p q r s t u v w x y z } + {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }  
4Lowercase lettersIt is a set of lowercase letters which lies in the range from (a to z)    { a b c d e f g h i j k l m n o p q r s t u v w x y z }.
5Uppercase lettersIt is a set of uppercase letters which lies in the range from: (A to Z)    {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }.
6Alphanumeric charactersIt is a set of Digits (0-9) , Lowercase letters (a-z) and Uppercase letters (A-Z).   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + a to z + A to Z }
7Alphabetic charactersset of Lowercase letters and Uppercase letters.
8Punctuation charactersset of @ # * ! "  $ % & ' ( )  + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
9Printable charactersset of Alphanumeric characters, Space characters. and Punctuation characters   {set of Digits (0-9) , Lowercase letters (a-z) and Uppercase letters (A-Z) + punctuation characters + space characters }  
10Control charactersoctal codes 000 through 037, and 177 (DEL).
11Graphical characters set of Alphanumeric characters and Punctuation characters.   {set of Digits (0-9), Lowercase letters (a-z) and Uppercase letters (A-Z) + punctuation characters}
12Blank charactersspaces and tabs.
13Space charactersset of tab, newline, vertical tab, return, and space etc.

Related Topics

Conditional Operator in C

In C programming language, the conditional operator (also known as the ternary operator) is a shorthand way of writing an if-else statement. The syntax of conditional operator in C is...

3 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.