No: C++ has no standard function named isnumber. To test whether one character is a decimal digit, use std::isdigit from <cctype>; to check whether a whole string represents a number, use a parsing function such as std::from_chars or write a character-by-character check for the exact format you want.
Is isnumber part of standard C++?
No. The standard C++ character-classification functions include std::isdigit, std::isalpha, and std::isspace, but not std::isnumber. You should not expect an expression such as std::isnumber(ch) to compile on a conforming, portable C++ implementation. A compiler may accept that name because its accompanying C library provides a nonstandard extension, but that is a platform-specific feature, not portable C++.
Some BSD-derived C libraries document an isnumber function. Its availability and precise behavior depend on the library and its documentation; code using it may fail when moved to another operating system or toolchain. If you encounter it in existing code, check which header and library define it, and confirm what that implementation considers a number. For new cross-platform C++, prefer standard facilities whose behavior is documented by the language library.
There is also a naming trap: Excel has a worksheet function called ISNUMBER, but that is not a C++ function. Likewise, C++ does not acquire an isnumber function simply by including <cctype>.
What std::isdigit actually checks
std::isdigit, declared in <cctype>, examines one character value. It reports whether that value is a decimal digit character; it does not parse a complete string, recognize a decimal point, or determine whether a value can be converted to an integer or floating-point number. Its return value is nonzero when the character matches and zero otherwise.
A minimal example is:
#include <cctype>
#include <iostream>
int main() {
char ch = '7';
if (std::isdigit(static_cast<unsigned char>(ch))) {
std::cout << "One decimal digit\n";
}
}
Here the test is about the single character '7'. It would be true for '0' through '9' in the ordinary C locale, but false for a minus sign, a decimal separator, or a letter. Consequently, std::isdigit('4') is a character test, not a general test of the text "4.25".
Safely calling cctype functions
The argument rule is easy to miss and matters for correctness. The character-classification functions from <cctype> accept an int, but the valid argument must be either EOF or a value representable as unsigned char. Passing a negative plain char value other than EOF causes undefined behavior. This can occur on systems where char is signed when the input byte has its high bit set.
When the source value is a char, cast it to unsigned char before calling std::isdigit or a similar function. That is why the example uses static_cast<unsigned char>(ch). If a value comes directly from a function such as std::getc, keep its result in an int and handle EOF before treating it as a character.
This safe cast prevents an invalid argument; it does not make std::isdigit a Unicode-aware test. In UTF-8 text, a non-ASCII character is encoded as multiple bytes, and checking each byte with std::isdigit does not classify the original Unicode character. For ASCII-formatted input, explicit checks against '0' and '9' can be clear. For international text, use a library and an encoding-aware approach suited to the data.
Checking whether a whole string contains digits
If the requirement is specifically “one or more decimal digit characters and nothing else,” check every character and decide what an empty string should mean. The following helper returns false for an empty string, and returns true only when every character passes the digit test:
#include <cctype>
#include <string>
bool containsOnlyDigits(const std::string& text) {
if (text.empty()) {
return false;
}
for (unsigned char ch : text) {
if (!std::isdigit(ch)) {
return false;
}
}
return true;
}
The range-based loop converts each element to unsigned char before the call. With this rule, "2048" passes, while "-2048", "20.48", and " 2048" do not. That does not mean those rejected strings are not numbers; it means they do not match the narrower “digits only” format.
Choosing empty-string behavior is important. A common alternative is std::all_of, which returns true for an empty range because there is no character that fails the predicate. If an empty input should be invalid, test text.empty() separately before calling it. Also consider whether leading zeros are allowed: the digits-only helper accepts "007", even if a particular application wants to reject that representation.
Parsing an integer is different from checking digits
When the goal is to obtain an integer value, parse the text rather than relying only on a digit check. Parsing can validate a number and produce its value; a digits-only check merely recognizes a restricted text pattern. C++17 provides std::from_chars in <charconv> for locale-independent conversion that does not allocate memory:
#include <charconv>
#include <string_view>
#include <system_error>
bool parseInt(std::string_view text, int& value) {
if (text.empty()) {
return false;
}
const char* first = text.data();
const char* last = first + text.size();
auto result = std::from_chars(first, last, value);
return result.ec == std::errc{} && result.ptr == last;
}
The cell-like layout here is not applicable; this is ordinary C++ code. For example, with text equal to "123", a successful call stores 123 in value and returns true. The error-code test catches conversion failures such as a value outside the range of int; the pointer test rejects unconsumed suffixes, so "123xyz" is not silently accepted as the integer 123.
Integer from_chars does not skip leading whitespace, and its grammar does not accept a leading plus sign. If those forms should be accepted, define that policy deliberately, normalize or handle the text, or choose a different parser. For a simple digits-only requirement, the earlier helper may be enough. For validation and conversion, parsing usually answers the more useful question: “Does all this input represent an integer in the target type’s range?”
Choosing a method for decimal and signed numbers
There is no universal “is a number” rule. Whether a string counts depends on the grammar your program intends to accept. For instance, the following strings each raise a policy question:
"-12": should a leading minus sign be allowed?"+12": should an explicit plus sign be allowed?"3.5": are decimal fractions allowed, and is the decimal separator a period?"1e6": should scientific notation be accepted?" 12 ": should surrounding whitespace be ignored or rejected?"12xyz": must the parser consume the entire input?
For floating-point text, use a suitable floating-point parser and verify both that conversion succeeded and that the complete input was consumed. C++17 specifies floating-point overloads of std::from_chars, though support can depend on the standard library implementation in use. Another option is std::strtod from <cstdlib>, which provides an end pointer; check that at least one character was converted and that the end pointer reaches the end of the input. Be aware that strtod follows the active C locale for its decimal-point conventions.
For formatted input or a user-facing grammar with locale requirements, a stream may be more appropriate, but stream extraction can accept a valid numeric prefix and leave trailing characters unread. Whichever method you choose, explicitly test for full consumption when extra text must make the input invalid. Parsing and validation should implement the same grammar, rather than validating with one rule and converting with another.
Common mistakes and misconceptions
- Calling a missing standard name:
std::isnumberis not part of standard C++. If code compiles only on one platform, identify the extension before depending on it. - Testing only the first character:
std::isdigit(text[0])says nothing about the rest of the string. A value such as"5oops"begins with a digit. - Using digit-only rules for numeric syntax: A negative integer includes a sign, and a decimal number may include a separator or exponent. Decide the accepted grammar instead of assuming all valid numbers contain digits alone.
- Forgetting range limits: A long sequence of digits can pass a character test while being too large for
int. A parser can report that conversion overflowed. - Passing a negative plain char: Cast to
unsigned charbefore usingstd::isdigit, unless the value is the specialEOFresult. - Assuming Unicode support:
std::isdigiton narrow characters is not a general Unicode decimal-digit classifier.
Frequently Asked Questions
Does C++ have an isnumber function?
No standard C++ function has that name. A particular system library may provide an extension, but portable code should use standard character tests or a parser such as std::from_chars.
What is the C++ equivalent of isnumber?
There is no single equivalent because “number” can mean one digit, a string of digits, or text that can be parsed as an integer or decimal. Use std::isdigit for one character, a loop for a digits-only string, and a parser for a numeric value.
Can std::isdigit check a complete number?
No. It checks one character at a time. To confirm that an entire string contains only digits, test every character and define how empty input should behave.
Does std::isdigit accept negative numbers or decimals?
No. The minus sign and decimal point are not decimal digit characters. Use a parser or a custom grammar if those forms are valid for your program.
Why cast a char to unsigned char before calling std::isdigit?
The cctype functions require an argument representable as unsigned char or equal to EOF. A negative signed char can violate that requirement, so casting avoids undefined behavior.
Is std::isdigit Unicode-aware?
No, not as a general UTF-8 Unicode classifier. UTF-8 characters can occupy multiple bytes; use an encoding-aware Unicode library if the program must recognize decimal digits across writing systems.
Practical rule of thumb
Start by stating what “number” means in the application. If it means a single decimal digit, call std::isdigit safely. If it means a string made exclusively of digits, inspect every character and set an empty-input policy. If it means a value the program can use, parse it, check errors and range, and ensure the parser consumed exactly the input you intend to accept. This distinction avoids nonportable isnumber calls and prevents many bugs caused by treating character classification as numeric conversion.
Related
- Excel Shortcut to Select Visible Cells in a Filtered Range
- Excel Save As Shortcut: Windows, Mac, and Web
- Excel Shortcut to Select All Data: What Ctrl+A Selects
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel Word Count Formula: Count Words in Cells and TextTo count words in a cell in Excel, use =IF(TRIM(A2)="",0,LEN(TRIM(A2))-LEN(SUBSTITUTE(TRIM(A2)," ",""))+1), replacing A2 with the cell you want…
- Excel How to Filter Columns: Steps and TipsTo filter columns in Excel for Microsoft 365 on Windows, select a cell in your data, choose Data…
- Excel MID and FIND Functions: How to Extract Text in ExcelWith the text INV-2026-047 in A2, use =MID(A2,FIND("-",A2)+1,4) to extract the year; the result is 2026. MID returns…
- How to Calculate Age in Excel for a Future Date with DATEDIFIf the date of birth is in A2 and the future date you want to measure age on…
- Excel Last Day of Month: Formula and ExamplesTo return the last day of the month for a date in Excel, use =EOMONTH(A2,0), where A2 contains…
- Power Query VLOOKUP: How to Match Tables in Excel InsteadPower Query does not have a VLOOKUP function; use Merge Queries to match rows from two tables and…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.