Like Tree3Likes
  • 2 Post By jimblumberg
  • 1 Post By laserlight

why 'isdigit' always return zero ?

This is a discussion on why 'isdigit' always return zero ? within the C Programming forums, part of the General Programming Boards category; 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 ...

  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
    2,917
    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 09:14 PM.
    laserlight and JavaLover like this.

  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
    22,267
    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).
    JavaLover likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03: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, 05:50 PM
  5. isdigit()
    By kermit in forum C Programming
    Replies: 3
    Last Post: 03-19-2004, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21