Parameters of isdigit() in C++
The isdigit function takes a single parameter:
- ch: This is the character to be checked. The function takes an int value, which is typically the ASCII value of the character.
Return Value of isdigit() in C++
The isdigit function returns a non-zero value (true) if the character is a numeric digit (0-9). Otherwise, it returns zero (false). This makes it easy to use in conditional statements to validate input.
Example of isdigit() Function
Here is a simple example demonstrating how to use the isdigit function in a C++ program.
C++
#include <iostream>
#include <cctype>
int main() {
char ch;
std::cout << "Enter a character: ";
std::cin >> ch;
if (isdigit(ch)) {
std::cout << ch << " is a digit." << std::endl;
} else {
std::cout << ch << " is not a digit." << std::endl;
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Explanation
- The program prompts the user to enter a character.
- It uses isdigit to check if the entered character is a digit.
- Based on the result, it prints whether the character is a digit or not.
Output:
Enter a character: 5
5 is a digit.
Exceptions of isdigit() in C++
The isdigit function does not throw any exceptions. It handles invalid input gracefully by returning false for any non-digit character. However, it is important to ensure that the input character is within the valid range to avoid undefined behavior.
What is isdigit() in C++?
The isdigit function is part of the <cctype> library in C++. It checks whether a given character is a decimal digit character. This function is particularly useful in parsing and validating numerical input in various applications.
Key Features
- Simple to use with a single character parameter.
- Returns a boolean value indicating whether the character is a digit.
- Part of the C++ Standard Library, ensuring compatibility across different platforms.
Undefined Behaviour of isdigit() in C++
The behavior of isdigit is undefined if the value of the argument is neither representable as an unsigned char nor equal to EOF. This means you should avoid passing values outside the range of unsigned char to isdigit to prevent unexpected results.
Frequently Asked Questions
Can isdigit check multiple characters at once?
No, isdigit checks only a single character at a time. To check multiple characters, you need to loop through each character and use isdigit on each one.
Is isdigit case-sensitive?
isdigit is not concerned with case sensitivity because it only checks for numeric digits (0-9), which are not case-sensitive.
What header file is required to use isdigit?
You need to include the <cctype> header file to use the isdigit function in C++.
Can isdigit be used with wide characters?
No, isdigit is designed for narrow characters. For wide characters, you can use the iswdigit function from the <cwctype> header.
What is the return type of isdigit?
The return type of isdigit is int. It returns a non-zero value (true) if the character is a digit and zero (false) otherwise.
Conclusion
The isdigit function in C++ is a valuable tool for validating numerical input. Its simple syntax and reliable performance make it a go-to choice for checking if a character is a digit. By understanding its parameters, return values, and potential pitfalls, you can effectively use isdigit in your C++ programs. Whether you are validating user input or parsing data, isdigit provides a straightforward and efficient solution.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.