ASCII Number

This is a discussion on ASCII Number within the C Programming forums, part of the General Programming Boards category; Er.....i want to detect if a character is a number between 0 and 9 i achieve this using ascii rite? ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    ASCII Number

    Er.....i want to detect if a character is a number between 0 and 9

    i achieve this using ascii rite? er......how do i specify this in the if statement?
    Only by the cross are you saved...

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    Code:
    #include <stdio.h>
    
    int main() {
        char test1 = 'a';
        char test2 = '2';
        
        function(&test1);
        function(&test2);
    
        return 0;
    }
    
    int function(char *test) {
        if(*test < '0' || *test > '9') {
            printf("not a character between 0-9\n");
            return 1;
        }
    
        printf("is a character between 0-9\n");
        return 0;
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The standard library function isdigit will return true if the character is a digit.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
       char *ptr, number[] = "*_42-Aa0";
       for ( ptr = number; *ptr != '\0'; ++ptr )
       {
          if ( isdigit(*ptr) )
          {
             printf("'%c' is a digit\n", *ptr);
          }
          else
          {
             printf("'%c' is not a digit\n", *ptr);
          }
       }
       return 0;
    }
    
    /* my output
    '*' is not a digit
    '_' is not a digit
    '4' is a digit
    '2' is a digit
    '-' is not a digit
    'A' is not a digit
    'a' is not a digit
    '0' is a digit
    */
    Last edited by Dave_Sinkula; 06-22-2003 at 10:52 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    GuiltySpark343
    Guest

    Re: ASCII Number

    Originally posted by fkheng
    Er.....i want to detect if a character is a number between 0 and 9

    i achieve this using ascii rite? er......how do i specify this in the if statement?
    Well, since you are talking about characters, and not numbers- you could just say:

    Code:
    void  foo(void)
       {
       char    myLetter;
    
       myLetter = 'j';              /* stuff a non-digit value in */
       if((myLetter <= '9') && (myLetter >= '0'))
          Success();
       else
          Failure();
       }
    d

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    i see, thanx, many alternatives to the code...

    about the isdigit, i recall using it, but somehow it could not detect number characters, i really do not know y...anyway, thanx ppl, the codes work!
    Only by the cross are you saved...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. Finding a number within a number
    By jeev2005 in forum C Programming
    Replies: 2
    Last Post: 01-10-2006, 07:57 PM
  5. Really need help....
    By Cris987 in forum C++ Programming
    Replies: 11
    Last Post: 02-29-2004, 06:42 PM

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