Thread: why 'isdigit' always return zero ?

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    4

    why 'isdigit' always return zero ?

    Code:
    int c=3;
    char c2='t';
    int f1,f2;
    
    f1=isdigit(c);
    printf("%d",f1);
    // output is 0
    
    f2=isdigit(c2);
    printf("%d",f2);
    // output is 0
    why 'isdigit' always return zero ?

    I thought, I will find something different in 'f2' ?

    I'm using C, not C++.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Well the short answer is that the integer 1 is not the same as the character '1' and therefore isdigit() will return zero. This is because the character '1' is actually equal to 49 (0x31) when cast to an int. See this link for isdigit() and from that link:
    Parameters
    c
    Character to be checked, casted to an int, or EOF.
    Jim
    Last edited by jimblumberg; 08-06-2012 at 08:14 PM.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    Quote Originally Posted by jimblumberg View Post
    Well the short answer is that the integer 1 is not the same as the character '1' and therefore isdigit() will return zero. This is because the character '1' is actually equal to 49 (0x31) when cast to an int. See this link for isdigit() and from that link:


    Jim
    Thanks Jim
    your reply, help me to understand the return of this function.

    I read this before, and it confuses me !
    Return Value

    A value different from zero (i.e., true) if indeed c is a decimal digit. Zero (i.e., false) otherwise.
    and since, there is no Boolean type in c
    I thought, true equal "number different from zero"
    false equal zero

    English is my second language

    best,

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by JavaLover
    and since, there is no Boolean type in c
    I thought, true equal "number different from zero"
    false equal zero
    Yes, that is correct. In particular, the default "true" value is 1 (but generally you should not rely on this particular value, except where it is guaranteed).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    just to clarify

    Code:
    char c1='3';
    char c2='k';
    int f1,f2;
    
    f1=isdigit(c1);
    printf("\n%d",f1);
    // output is 1
    f2=isdigit(c2);
    printf("\n%d\n",f2);
    // output is 0
    so, now if I get 1 means the Var is a digit, zero if it is not

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    Quote Originally Posted by laserlight View Post
    Yes, that is correct. In particular, the default "true" value is 1 (but generally you should not rely on this particular value, except where it is guaranteed).

    Thanks Laserlight for the advice, I'll keep that in mind

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using isdigit()
    By IndioDoido in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 08:39 PM
  2. isdigit
    By quasigreat in forum C Programming
    Replies: 2
    Last Post: 06-27-2008, 02:57 PM
  3. Using isdigit
    By Acolyte in forum C Programming
    Replies: 6
    Last Post: 11-07-2007, 05:30 PM
  4. using isdigit()
    By Beowolf in forum C Programming
    Replies: 7
    Last Post: 09-10-2007, 04:50 PM
  5. isdigit()
    By kermit in forum C Programming
    Replies: 3
    Last Post: 03-19-2004, 09:59 PM