Thread: using isdigit()

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Question using isdigit()

    Hello,

    I can't seem to correctly use isdigit(). I have a variable that is assigned the value of argv[1], a value from the command-line. I want to check to see if that value is an integer or not. Here is what I have:

    Code:
    #include <ctype.h>
    
    [...]
    if(isdigit(myVar) == 0) {
            printf("myVar is not a digit.");
    } else {
            printf("myVar is a digit.");
    }
    But not matter the value of myVar, be it "12" or "abcd", it keeps returning 0 for isdigit(). Any help? Like I said above, basically I am just trying to figure out if the value of argv[1] is a number or not.

    Thanks!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    isdigit() checks characters, not strings.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    When I run it with myVar = "b" and myVar = 5 the output is the same - a zero.

    Is there a way to check a string (like argv[1]) and see if it contains just numbers?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    well yeah, namely using isdigit on every character of the string.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, if you enable warnings, the compiler should tell you that you're doing it wrong.

    Second, if you want to check if a string contains only digits, then you need to walk from the beginning to the end of the string, checking each char if it's a digit. Or, if you are going to make it into an integer anyways, you could use something like strtol() that converts to integer and tells you where it stopped converting, so you can check "if there's anything left over".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    EDIT: never mind, it is working now. Thanks!
    Last edited by Beowolf; 09-10-2007 at 04:30 PM. Reason: fixed

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
        char myVar = 'a';
    
        if(isdigit(myVar) == 0) {
             printf("myVar is not a digit.");
        } else {
             printf("myVar is a digit.");
        }
    
        return 0;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So does your compiler complain when you do
    isdigit("5");

    What about
    isdigit('5');
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. isdigit woes
    By kryonik in forum C Programming
    Replies: 3
    Last Post: 10-14-2005, 05:10 PM
  2. isdigit()
    By kermit in forum C Programming
    Replies: 3
    Last Post: 03-19-2004, 09:59 PM
  3. I read the FAQ on isdigit() but...
    By CaptainJack in forum C Programming
    Replies: 5
    Last Post: 03-16-2004, 06:03 AM
  4. #include cctype and the isdigit
    By nextus in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2002, 07:55 PM
  5. Correct use of isdigit
    By DocDroopy in forum C Programming
    Replies: 3
    Last Post: 08-05-2002, 07:22 AM