C++

Isdigit C++

The isdigit() function is used in C and C++ programming languages. The purpose of this function is to check if the character that passes through it is a digit or is any other character. It always returns a value that is non-zero if the character is a digit; otherwise, it returns 0. Such situations mainly occur in programming while creating a source code when we need to separate numeric and alphabetic values from the string. Then we prefer to use this feature along with the character array of having other operators in the form of characters. This article will contain some elementary examples related to isdigit().

Features of isdigit()

  • The isdigit() function is declared in the header file <type.h>. That’s why we need to use this library in our source code, either in C or C++.
  • This function takes only one argument of integer data type and returns the value in the form of an integer.
  • The function is used to check if the character it takes as an argument in the parameter is in a numeric value from 0 to 9 or not.
  • Even though we know that this function takes an argument in integer, a single character can also be passed through it; inside the function, the character is converted to ASCII value for checking.

Isdigit() Syntax

The syntax of the isdigit() function is:

#Isdigit( int ch);

It takes only a single character that is ‘ch’; this character is checked. First of all, it is preferred to use a direct integer, but if the user adds a character instead of an integer, it is cast to an integer type and then checked through the isdigit() function.

The isdigit method does not work properly for decimal or float numbers. Because for them, it returns false.

Return value

The isdigit() function returns the value:

  • A non-zero integer numeric value if we use a character as a digit. This also acts as a Boolean flag. 1 is returned if the character is a digit. It acts as a TRUE situation.
  • On the other hand, as a Boolean, it returns 0 when the condition is false, and the character is not a digit.

Difference between isdigit() , numeric() and decimal()

The main difference between these three is that the isdigit() function also supports the decimals and subscripts. Whereas the decimal() function only supports the decimal numbers. Isnumeric() function also has support for subscripts and superscripts.

Undefined behavior of isdigit()

The behavior of the isdigit() function is said to be undefined if the value of the entered character is unable to be representable as an unsigned character. Or secondly, if the value of the character is not equal to EOF.

Sample

We will verify if the character used is a digit or not. This is a simple usage of this built-in function. This will act as a Boolean here. If the digit is found, it will return 1, and in the second case, 0 will be returned. Simply we will pass a digit ‘5’ as a parameter to the function

Isdigit('5');

Execute the source code by utilizing the G++ compiler. This compiler is used to execute C++ programs in the Ubuntu operating system.

$ g++ -o file file.c

$ ./file

The resultant value is displayed.

Example 1

This example contains a character that is assigned by a number. Then we use an if-else statement to check if the used character is a digit or not. If it is a digit, a message will be displayed. And if it is not, then again, the system displays that the value is not numeric.

Isdigit(ch);

Now we will compile the code and execute it as we have used a numeric value to see that the message will be displayed that the character is numeric.

Example 2

This example involves the usage of a string having mixed characters, both alphabets and numbers as well. For string input, we have used the library of the string to use all the built-in functions of strings.

#include <cstring>

In the main program, initialize the string. Here we have taken an alphabet to assure the presence of digits. As it is a whole string so to tackle this, we have used a FOR loop to check each element on each index of the character array. The function of string strlen(str) is used for the length of the string.

Check = isdigit(str[i]);

The function will check the digits present in the string, and then the number will be stored in the variable check. Then we applied a check through if-statement to check if the variable ‘check’ is not empty and then printed all the digits present inside the variable.

The strings we have used contain random characters having operators as well. So on execution, you will only get the digital numeric values.

Example 3

Like isdigit, isalpha is also a feature used in the C++ programming language to check the alphabets present in the input string. So inside the main program, we will again take a string having alphabets and numbers. Both functions will work to separate alphabets and numbers from the string. Both alphabets and numbers are the integer type variables to store the resultant values, initialize as 0 initially. By using a FOR loop, we can access each character of the array. First, ‘isalpha’ will check all the alphabets.

It will work so that if an alphabet is found, the variable will be incremented as a counter. A similar case is for isdigit; the number variable will be incremented when it finds any number in the string. Both these counters will display the total numbers.

You can see that the string contains 3 alphabets and 4 digits.

Example 4

This example has user interaction. The entered value will be checked by the system whether it is a digit or not. Then the message is displayed by the program.

The main program has the variable that is an integer type to check if the entered character is a digit. Then a message is displayed so that the user will enter a character. This character is checked through the if statement by using the isdigit() function. If it is a digit, the message is displayed that it is obtained.

Execute the source code. First, we will enter a digit on displaying the message. The system will check according to the program. Hence a message of the digit is obtained.

Now we will use an alphabet string to check the results. So the resultant value shows that the entered value is not a digit.

Conclusion

‘isdigit C++’ is a function used to check if the string or a character it takes as a parameter is a digit or contains one or more digits in it or not. This is a simple function used in the C++ programming language. Some basic features are discussed here that refer to its usage and working. We have implemented some Ubuntu system examples that show the resultant values according to the isdigit function.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.