The C language provides a number of functions in its libraries that allow you to determine whether the data type of a variable is of a particular type. In this language, there are also functions that determine the properties and classify the data within the same type. For example, the ctype library provides a set of functions that allow you to determine whether the character that is entered in the input argument is numeric or alphanumeric, digit, graphics, control, uppercase, and so on.
In this Linx Hint article, we will show you how to use the isdigit() function of this library using the Linux gcc. We use the function in practical examples that we prepared for you. In doing so, we use the code snippets and images to apply the use of isdigit() in different cases.
We will also show you a complete theoretical description of this function, its syntax, input and output arguments, and the data type of each of them.
Isdigit Function Syntax in C
Description of the Isdigit Function in C
The isdigit() function in C determines whether the “c” input character corresponds to decimal values from 0 to 9 in ASCii code. These types of functions are also a great resource to retrieve the information from string fragments or text files and process that data.
These types of functions are often used to complement the functions such as getchar() or getch() which read a character from a stream and return an integer. They also return a character and an error code. Getchar() may return EOF (which is defined as a negative implementation-defined constant) via a return value to indicate that the input stream has terminated.
For the following expression, isdigit() returns a result of “a” equal to “0” if “b” does not contain a character of “digit” type. If “b” contains a character of “digit” type, this function returns a result that is not equal to zero.
The isdigit() function belongs to the “ctype.h” header and must be declared before using this function as shown in the following fragment:
Once we declare the header in the “.c” file, we can implement the ceil() and any of its functions.
Next, we compile a code fragment in which we use two variables, a and b, with characters representing the digits and letters, respectively. Then, we send them as input arguments to isdigit().
Using the printf() function, we will see the result that is returned by each of the calls with the different characters that are sent as input in the command console.
#include <ctype.h>
int main()
{
char a ='3';
char b ='a';
int c;
c = isdigit(a);
printf ("Is numeric character: %d", c);
c = isdigit(b);
printf ("\nIs non-numeric character: %d\n", c);
return 0;
}
As the following figure shows, the result that is returned by isdigit() is equal to 0 for variable “a”, while it is not equal to 0 for variable “b” which contains a non-numeric character:
Example: Isdigit() as a Condition in the If Conditionals
These types of functions that are used to determine the data type of a variable alone do not provide a practical solution. But when the result of these functions is added as a condition in any type of conditional, we can execute the code or generate a return depending on whether the data type that is entered is the correct one for a particular process. This is especially useful when we create our own functions as it avoids data incompatibility errors or erroneous results if the wrong data is passed in the input arguments.
In this example, we create a simple console application where we enter a character, retrieve it with the scanf() function, and then use the isdigit() for an “if” condition to determine whether the character that is entered is numeric or not. When we press “ENTER”, the “The character is numeric” message is displayed if it is numeric. Otherwise, the “The character is not numeric” message is displayed.
Copy and paste the following code for this example into your “.c” file. Compile and run the program, type a character, and press Enter. To exit the application, press Ctrl + C:
#include <ctype.h>
void main ()
{
char a [2];
while (1)
{
scanf ("%s", &a[0]);
if ( isdigit ( *a ) != 0 )
{
printf ("The characters is numeric \n");
}
else
{
printf ("The characters is no numeric \n");
}
}
return;
}
The figure shows the use of this function in the “if” conditionals and the results for each case.
Conclusion
Isdigit() is one of the functions that is defined in the” ctype.h” header which is used to classify the characters into their various subtypes such as numeric, alphanumeric, uppercase, and so on. In this Linux Hint article, we explained everything about the isdigit() function. To help you master and understand this resource, we created practical examples and code fragments that explain its use step-by-step. We also added the images that show the implementation of the examples in the command console. We hope you found this C language article helpful. See other Linux Hint articles for more tips and information.