What is the purpose of the isnumber function in C++?

In C++, the isnumber() function is used to check if a given character is a number. It is a standard C library function and is included in the header file.

The prototype of the isnumber() function is as follows:

int isnumber(int c);

This function takes an integer parameter c, which must be an unsigned character or EOF (indicating end of file). If the parameter c is a numerical character (0-9), it returns a non-zero value, otherwise it returns zero. This function is typically used in combination with the isdigit() function to validate if the input is a numerical character.

The isnumber() function can be used to check if a character is a number, for example:

#include <iostream>
#include <ctype.h>

int main() {
    char ch = '5';
    if (isnumber(ch)) {
        std::cout << "Character is a number.\n";
    }
    else {
        std::cout << "Character is not a number.\n";
    }

    return 0;
}

Output:

Character is a number.

In the example above, we used the isnumber() function to check if the variable ch is a numeric character. Since the value of ch is the character ‘5’, the isnumber() function returned a non-zero value, indicating that ch is a numeric character.

bannerAds