isdigit() not working

This is a discussion on isdigit() not working within the C Programming forums, part of the General Programming Boards category; Code: #include <stdlib.h> #include <stdio.h> int main(){ long x; scanf("%ld",&x); if(!isdigit(x)) printf("error"); return 0; } inputs : a , 1 ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    isdigit() not working

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    int main(){
      long x;
      scanf("%ld",&x);
      if(!isdigit(x))
         printf("error");
    return 0;
    }
    inputs : a , 1
    both inputs prints error
    if that problems occurs just because its long how can i check a long type if all it's digits are decimal
    Last edited by cable; 11-03-2011 at 11:36 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    isdigit() takes an ASCII character value. This is a little confusing since it is in the form of an "int". Try this:

    Code:
    	printf("%d %d\n", 'a', isdigit('a'));
    	printf("%d %d\n", '1', isdigit('1'));
    And if you are not aware of it:

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
    ASCII - Wikipedia, the free encyclopedia

    These are the integer->character values used by isdigit() and C char types.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Because it should be
    Code:
    if ( scanf("%ld",&x) == 1 ) {
      // the longest sequence of 0 to 9 was converted to a long int
      // after skipping leading white space.
      // This also includes (and is undiagnosed) excessively long sequences
      // of 0 to 9 which would cause numeric overflow.
    } else {
      // first char of input wasn't 0 to 9
    }
    So typing "123abc" extracts 123 as an integer, and leaves "abc" for next time.
    Whereas typing "abc123" doesn't do anything at all, and the input stream is still "abc123"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by cable View Post
    how can i check a long type if all it's digits are decimal
    Better question: How can a long hold anything but a number?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isdigit()
    By a.mlw.walker in forum C Programming
    Replies: 10
    Last Post: 04-14-2009, 12:43 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. isdigit
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 04-02-2007, 04:14 PM
  5. isdigit ()
    By mackol in forum C++ Programming
    Replies: 8
    Last Post: 01-18-2003, 11:03 PM

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