isdigit() in C++ Last Updated : 26 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The isdigit() function in C++ checks whether a given character is a digit or not. Let's take a look at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { char c = '5'; if (isdigit(c)) cout << c << " is a Digit"; else cout << c << " is not a Digit"; return 0; } Output5 is a DigitExplanation: The isdigit() function returns a non-zero value if the character is a digit. Since c is a digit, it returns a non-zero (true) value.This article provides an overview of the syntax, working and use cases of the isdigit() function in C++.Table of ContentSyntax of isdigit()Working of isdigit() FunctionExamples of isdigit()Sum of Digits in a StringPhone Number ValidationSyntax of isdigit()The isdigit() function is defined inside the <cctype> header file.isdigit(c);Parameterc: Character to check if it is a digit.Return ValueReturns non-zero (true) if the character is a digit ('0'–'9').Returns 0 (false) otherwise.Working of isdigit() FunctionAs all the characters in C++ are represented by their ASCII values and all the characters representing numeric digits lies in the range [48, 57]. The isdigit() function works by checking if the ASCII value of the given character lies in the range from 48 to 57 (or '0' to '9').If the character falls within this range, it returns a non-zero value (true).Otherwise, it returns 0 (false).Examples of isdigit()Below are some common use cases of the isdigit() function in C++ that demonstrate its use in validating and processing numeric characters.Sum of Digits in a String C++ #include <bits/stdc++.h> using namespace std; int main() { string s = "g1f2g3"; int sum = 0; for (char c : s) { // If the given character is a digit // Convert it to int and add if (isdigit(c)) sum += c - '0'; } cout << sum; return 0; } Output6Phone Number ValidationAs a phone number should only contain numbers, a string representing a phone number should only contains characters that represent numbers. C++ #include <iostream> #include <cctype> using namespace std; int main() { // Here alphabet 'O' is used instead of '0' string ph_num = "9124782O21"; // Check if all characters are numbers for (char c : ph_num) { if (!isdigit(c)) { cout << "Invalid number!"; return 0; } } cout << "Valid number"; return 0; } OutputInvalid number! Comment More infoAdvertise with us Next Article isdigit() in C++ D deepanshus2qvy Follow Improve Article Tags : C++ CPP-Functions Practice Tags : CPP Similar Reads iswdigit() function in C/C++ The iswdigit() is a built-in function in C++ STL which checks if the given wide character is an decimal digit character or not. It is defined within the cwctype header file of C++. The characters from 0 to 9 i.e.0, 1, 2, 3, 4, 5, 6, 7, 8, 9 are classified as decimal digits. Syntax: int iswdigit(ch) 2 min read std::to_string in C++ In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_strin 1 min read iota() in C++ In C++, iota() is a library function used to fill a range of elements with increasing values starting from the given initial value. It assigns the starting value to the first element and then increments it once for the next element and so on.Let's take a look at an example:C++#include <bits/stdc+ 3 min read iswxdigit() function in C/C++ The iswxdigit() is a built-in function in C/C++ which checks if the given wide character is a hexadecimal digit character or not. It is defined within the cwctype header file of C++. The available hexadecimal numeric characters are: Digits (0 to 9) Lowercase alphabets from a to f Uppercase alphabets 2 min read isgreater() in C/C++ In C++, isgreater() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions.isgreater() function used to check whether the 1st argument given to the function is greater than the 2nd argument given to the function or not. Mean 3 min read Like