In C, the isdigit() function is a standard library function declared in the <ctype.h> header file. It is used to determine whether a given character is a digit or not. Here are the rules for isdigit():
- The
isdigit()function takes a single argument of typeint, which represents the character to be tested. - The function returns an integer value of non-zero if the character is a digit (0-9), and returns 0 if the character is not a digit.
- The
isdigit()function does not distinguish between different character sets or encodings. It operates based on the character’s ASCII value. - The function only works with individual characters, not strings or sequences of characters.
Here’s an example usage of isdigit():
<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><stdio.h></span></span>
<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><ctype.h></span></span>
<span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span> {
<span class="hljs-type">char</span> ch = <span class="hljs-string">'5'</span>;
<span class="hljs-keyword">if</span> (<span class="hljs-built_in">isdigit</span>(ch)) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c is a digit.\n"</span>, ch);
} <span class="hljs-keyword">else</span> {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c is not a digit.\n"</span>, ch);
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
<span class="hljs-number">5</span> <span class="hljs-keyword">is</span> a digit.
In the example above, the isdigit() function is used to check whether the character ch is a digit. Since it is the digit ‘5’, the condition evaluates to true, and the message “5 is a digit.” is printed.
