Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax of isdigit() in C++
3.
Parameters of isdigit() in C++
4.
Return Value of isdigit() in C++
5.
Example of isdigit() Function
5.1.
C++
5.2.
Explanation
6.
Exceptions of isdigit() in C++
7.
What is isdigit() in C++?
7.1.
Key Features
8.
Undefined Behaviour of isdigit() in C++
9.
Frequently Asked Questions
9.1.
Can isdigit check multiple characters at once? 
9.2.
Is isdigit case-sensitive? 
9.3.
What header file is required to use isdigit?
9.4.
Can isdigit be used with wide characters? 
9.5.
What is the return type of isdigit?
10.
Conclusion
Last Updated: Aug 25, 2024
Easy

isdigit in C++

Author Riya Singh
0 upvote

Introduction

The isdigit function in C++ is a useful tool when you need to check if a character is a numeric digit. This function is part of the C++ Standard Library and is often used in situations where input validation is required. 

isdigit in C++

For instance, if you want to ensure that a user enters only numeric characters, isdigit can help.

Syntax of isdigit() in C++

The syntax for the isdigit function is straightforward. It is declared in the <cctype> header file, which must be included in your program to use this function

#include <cctype>

int isdigit(int ch);

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++

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass