user3237895
user3237895

Reputation: 95

Unexpected Results Using isdigit(x)

I am using the following code. I expect the output to be "Yes", but I instead get "No." I must be missing something very simple and fundamental.

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int n = 3;

    if (isdigit(n))
    {
        printf("Yes\n");
    }

    else
    {
        printf("No\n");
    }

    return 0;
}

Upvotes: 1

Views: 150

Answers (7)

barak manos
barak manos

Reputation: 30136

Here is the implementation of function isdigit:

int isdigit(char c)
{
    return '0' <= c && c <= '9';
}

In simple words, function isdigit takes a char argument as input:

  • If the input represents a decimal digit in ASCII format, then the function returns 1.

  • If the input does not represent a decimal digit in ASCII format, then the function returns 0.

When you call function isdigit with an int argument, the argument is first truncated to a char. That doesn't make any difference in your example, since 3 fits into a char, so no information is lost due to the truncation. However, since 3 does not represent any decimal digit in ASCII format, the return-value of isdigit(3) is 0.

To summarize the above:

  • The return-value of isdigit('0') is 1.
  • The return-value of isdigit('1') is 1.
  • ...
  • The return-value of isdigit('9') is 1.
  • In all other cases, the-return value of isdigit(...) is 0.

Your mistake probably stems from the assumption that '3' == 3. If you want to check whether or not an int variable stores a single-decimal-digit value, then you can implement a slightly different function:

int isintdigit(int i)
{
    return 0 <= i && i <= 9;
}

Upvotes: 0

pmg
pmg

Reputation: 108978

The string "\03\t\nABCabc123" is composed of 12 characters. The first 9 characters all return false when applied to the isdigit() function. The last 3 all return true;

#include <ctype.h>
#include <stdio.h>

int main(void) {
    char data[] = "\03\t\nABCabc123";
    // data[0] = 3;
    char *ptr = data;
    while (*ptr) {
        printf("%4d --> %s\n", *ptr, isdigit((unsigned char)*ptr) ? "true" : "false");
        ptr++;
    }
    return 0;
}

Upvotes: 0

Maxime Ch&#233;ramy
Maxime Ch&#233;ramy

Reputation: 18831

man isdigit:

These functions check whether  c,  which  must  have  the  value  of  an
unsigned  char or EOF, falls into a certain character class according to
the current locale.

3 is an integer, not a character. In fact, 3 is also the value of a character that is not a digit.

Upvotes: 0

haccks
haccks

Reputation: 106012

isdigit expects a character. If the character passed is digit then it returns non zero. 3 is an ASCII value of non-printable character, i.e it doesn't represent a digit and that's why your else body gets executed.

Upvotes: 0

ajay
ajay

Reputation: 9680

isdigit checks whether the character passed to it is a numeric character. Therefore, its argument should be char type or int type which is the code of a character.

Here, you are passing 3 to isdigit. In ASCII, 3 is the code of the character ETX (end of text) which is a non-numeric character. Therefore, isdigit(3) returns false.

Upvotes: 2

Crowman
Crowman

Reputation: 25908

'3' is not a digit, here, in the way isdigit() considers them. Change int n = 3 to int n = '3'. If your system uses ASCII, for instance, 3 is the end of text marker, whereas 51, equivalent to '3', is the actual character three.

Upvotes: 1

Deduplicator
Deduplicator

Reputation: 45654

isdigit() expects a character code, while you expect it to accept a plain number.
Those expectations do not mesh well.

Character literals:

  • '0' ordinal 0x30 -- 48
  • '1' ordinal 0x31 -- 49
  • '2' ordinal 0x32 -- 50
  • ... You get the drift

Upvotes: 2

Related Questions