isdigit

Discussion in 'C Programming' started by mdh, Oct 6, 2006.

  1. mdh

    mdh Guest

    Could I get some help as to understanding why I am not getting a result
    I expect.


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

    int main (){
    int i,j,k,c;
    i=9;
    c='A';


    j=isdigit(9);
    k=isdigit('A');


    printf("\nThe value of \"isdigit(%d)\" is %d", i,j);
    printf("\nAnd the value of \"isdigit('%c')\" is %d\n", c,k);

    return 0;
    }



    Output:

    The value of "isdigit(9)" is 0
    And the value of "isdigit('A')" is 0

    Expected:

    The value of "isdigit(9)" is 0
    And the value of "isdigit('A')" is 1
     
    mdh, Oct 6, 2006
    #1
    1. Advertising

  2. mdh

    Neil Guest

    mdh wrote:
    > Could I get some help as to understanding why I am not getting a result
    > I expect.
    >
    >
    > #include <stdio.h>
    > #include <ctype.h>
    >
    > int main (){
    > int i,j,k,c;
    > i=9;
    > c='A';
    >
    >
    > j=isdigit(9);
    > k=isdigit('A');
    >
    >
    > printf("\nThe value of \"isdigit(%d)\" is %d", i,j);
    > printf("\nAnd the value of \"isdigit('%c')\" is %d\n", c,k);
    >
    > return 0;
    > }
    >
    >
    >
    > Output:
    >
    > The value of "isdigit(9)" is 0
    > And the value of "isdigit('A')" is 0
    >
    > Expected:
    >
    > The value of "isdigit(9)" is 0
    > And the value of "isdigit('A')" is 1
    >

    'A' is not a digit
    9 is not a digit

    '9' is a digit
     
    Neil, Oct 6, 2006
    #2
    1. Advertising

  3. mdh

    Cong Wang Guest

    On Oct 6, 2:02 pm, "mdh" <> wrote:
    > Could I get some help as to understanding why I am not getting a result
    > I expect.
    >
    > #include <stdio.h>
    > #include <ctype.h>
    >
    > int main (){
    > int i,j,k,c;
    > i=9;
    > c='A';
    >
    > j=isdigit(9);
    > k=isdigit('A');
    >
    > printf("\nThe value of \"isdigit(%d)\" is %d", i,j);
    > printf("\nAnd the value of \"isdigit('%c')\" is %d\n", c,k);
    >
    > return 0;
    >
    > }Output:
    >
    > The value of "isdigit(9)" is 0
    > And the value of "isdigit('A')" is 0
    >
    > Expected:
    >
    > The value of "isdigit(9)" is 0
    > And the value of "isdigit('A')" is 1


    Do you consider 'A' as a hexadecimal digit? If so, use function
    isxdigit() instead of isdigit().
     
    Cong Wang, Oct 6, 2006
    #3
  4. mdh

    mdh Guest


    > 'A' is not a digit
    > 9 is not a digit
    >
    > '9' is a digit



    it's getting late...thank you
     
    mdh, Oct 6, 2006
    #4
  5. mdh

    Rnags Guest

    Answer; Since c is a integer variable,when we try to store the
    charecter in the integer variable Ascii value of that charecter is
    stored in the variable,since Ascii value of the charecter is also digit
    hence the isdigit()
    function returns true
     
    Rnags, Oct 6, 2006
    #5
  6. mdh

    shafi Guest

    > j=isdigit(9);
    > k=isdigit('A');


    Hi,
    The isdigit() function takes an argument of character type or EOF and
    it return non zero value if
    the passing argument is characters '0' , '1' . . . '9' else it returns
    zero.
    Now consider your actual out put.

    1. The first part of the out put is true as you have supplied wrong
    argument i.e. a integer
    insite of supplying char.
    2. Second part is also true. No need to explain.

    Regards,
    Shafi

    "the difficult we do immediately, the impossible takes a little
    longer", [US Army
     
    shafi, Oct 14, 2006
    #6
  7. "shafi" <> writes:
    >> j=isdigit(9);
    >> k=isdigit('A');

    >
    > The isdigit() function takes an argument of character type or EOF and
    > it return non zero value if
    > the passing argument is characters '0' , '1' . . . '9' else it returns
    > zero.


    More precisely,

    ... the argument is an int, the value of which shall be
    representable as an unsigned char or shall equal the value of the
    macro EOF. If the argument has any other value, the behavior is
    undefined.

    This is an important distinction. Something like this:

    char c = some_value;
    isdigit(c);

    can invoke undefined behavior if (1) plain char is signed, and (2) the
    value of c is negative (and not equal to EOF). For arguments other
    than EOF, it's often a good idea to cast the argument:

    char c = some_value;
    isdigit((unsigned char)c);

    (This is one of the few cases where a cast is actually a good idea.)

    Or, in this case, you could avoid the cast by changing the declaration
    of c:

    unsigned char c = some_value;
    isdigit(c);

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 14, 2006
    #7
  8. On Fri, 2006-10-13 at 22:22 -0700, shafi wrote:
    > > j=isdigit(9);
    > > k=isdigit('A');

    >
    > Hi,
    > The isdigit() function takes an argument of character type or EOF and
    > it return non zero value if
    > the passing argument is characters '0' , '1' . . . '9' else it returns
    > zero.


    isdigit(c) function takes an argument of type int that must be in the
    range of unsigned char or EOF. It returns nonzero on '0'...'9',
    otherwise returns 0.

    > Now consider your actual out put.
    >
    > 1. The first part of the out put is true as you have supplied wrong
    > argument i.e. a integer
    > insite of supplying char.


    Wrong. 9 is in the range of unsigned char, and therefore is perfectly
    valid. Whether the expression evaluates to true or not depends on the
    charset.

    > 2. Second part is also true. No need to explain.
    >


    Wrong. 'A' is not a digit.

    --
    Andrew Poelstra <http://www.wpsoftware.net/projects/>
     
    Andrew Poelstra, Oct 14, 2006
    #8
  9. mdh

    pete Guest

    Andrew Poelstra wrote:
    >
    > On Fri, 2006-10-13 at 22:22 -0700, shafi wrote:
    > > > j=isdigit(9);
    > > > k=isdigit('A');

    > >
    > > Hi,
    > > The isdigit() function takes an argument
    > > of character type or EOF and
    > > it return non zero value if
    > > the passing argument is characters
    > > '0' , '1' . . . '9' else it returns zero.

    >
    > isdigit(c) function takes an argument of type int that must be in the
    > range of unsigned char or EOF. It returns nonzero on '0'...'9',
    > otherwise returns 0.
    >
    > > Now consider your actual out put.
    > >
    > > 1. The first part of the out put is true as you have supplied wrong
    > > argument i.e. a integer
    > > insite of supplying char.

    >
    > Wrong. 9 is in the range of unsigned char, and therefore is perfectly
    > valid. Whether the expression evaluates to true or not depends on the
    > charset.


    It doesn't depend on the char set.
    isdigit is one of the ctype functions
    that does not have locale specific behavior.

    --
    pete
     
    pete, Oct 14, 2006
    #9
  10. pete <> writes:
    > Andrew Poelstra wrote:

    [...]
    >> Wrong. 9 is in the range of unsigned char, and therefore is perfectly
    >> valid. Whether the expression evaluates to true or not depends on the
    >> charset.

    >
    > It doesn't depend on the char set.
    > isdigit is one of the ctype functions
    > that does not have locale specific behavior.


    The value of isdigit(9) depends on the character encoding, which can
    vary from one implementation implementation to another. (I don't know
    whether "char set" is the right term for this.)

    I believe it returns 0 on all existing implementations, but 9 could
    theoretically be the encoding for some digit (but it can't be the
    encoding for '9', since the digits are contiguous and '\0' can't be a
    digit).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Oct 14, 2006
    #10
  11. mdh

    pete Guest

    Keith Thompson wrote:
    >
    > pete <> writes:
    > > Andrew Poelstra wrote:

    > [...]
    > >> Wrong. 9 is in the range of unsigned char, and therefore is perfectly
    > >> valid. Whether the expression evaluates to true or not depends on the
    > >> charset.

    > >
    > > It doesn't depend on the char set.
    > > isdigit is one of the ctype functions
    > > that does not have locale specific behavior.

    >
    > The value of isdigit(9) depends on the character encoding, which can
    > vary from one implementation implementation to another. (I don't know
    > whether "char set" is the right term for this.)
    >
    > I believe it returns 0 on all existing implementations, but 9 could
    > theoretically be the encoding for some digit (but it can't be the
    > encoding for '9', since the digits are contiguous and '\0' can't be a
    > digit).


    I've been very sloppy in my postings lately.
    I saw 9 but read it as '9'.

    --
    pete
     
    pete, Oct 14, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach

    isdigit() for characters greater than 127

    Alf P. Steinbach, Oct 9, 2004, in forum: C++
    Replies:
    4
    Views:
    3,269
    James Gregory
    Oct 9, 2004
  2. Replies:
    9
    Views:
    575
    Andreas Leitgeb
    Feb 9, 2007
  3. Carramba

    passing array to isdigit()

    Carramba, Feb 9, 2005, in forum: C Programming
    Replies:
    15
    Views:
    813
    Michael Mair
    Feb 21, 2005
  4. MooMaster
    Replies:
    2
    Views:
    412
    Dan Bishop
    May 1, 2008
  5. Donkey Hottie

    Re: print problem using isDigit

    Donkey Hottie, Sep 29, 2008, in forum: Java
    Replies:
    4
    Views:
    392
    RedGrittyBrick
    Sep 29, 2008
Loading...

Share This Page