is a number? C function

This is a discussion on is a number? C function within the C Programming forums, part of the General Programming Boards category; Hello. Is there a function in C that check if a string is an int??. Thanks for your help...

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

    is a number? C function

    Hello.

    Is there a function in C that check if a string is an int??.

    Thanks for your help

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You could use strtol to find out. If it returns zero check if the string is 0, if not then its not a number.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Thanks a lot. But is there a faster way? Like a isnumber() function?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What exactly do you want to achiev? strtol() converts the string into a long integer - if you don't actually need that, then you can check if each character "isdigit()", but if you are interested in using it as an integer value, you may just as well convert it and check if the conversion worked, and regardless of whether the result is zero or not, check with this method:
    1. check that the passed in "endptr" is different from the original string (this ensures the string wasn't empty).
    2. check that endptr points to a end-of-string zero ('\0', NUL).

    That ensures that the passed in string was accepted as a whole - whether it contained only zero or something else.



    --
    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.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    2. check that endptr points to a end-of-string zero ('\0', NUL).
    Yeah I thought of that, but I wasent completely sure where the endptr would end up.

    So yeah go with what matsp suggested its not a lot of code, and speed shouldent be an issue for this kind of stuff.

    You could even wrap it into your own isnumber() function.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    What I want to do is to check that a given string is a number and that this number is less than 900. I didn't use isdigit() function because I want to check the whole string.

    For the second check I'm using:

    if (strtol(string, NULL, 10) < 900) ...

    So matsp, correct if I'm wrong, what you are proposing is this:


    if (strtol(string, endptr 10) < 900)
    if (!g_strrstr(string, endptr)....

    Thanks for your help

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would do the check like this:
    Code:
    char *endPtr;
    int number = strtol(string, &endptr, 10);   
    // Two possible "endptr" scenarios:
    // Nothing got parsed: string == endptr
    // Or the whole string didn't get parsed: *endptr != '\0'
    if (string == endptr || *endptr != '\0') 
    {
        // Not a valid number
    }
    if (number > 900)
    {
        // Too big number 
    }
    I'm not sure if g_strstr will do the same thing or not, but it seems overly complicated compared to the first if-statement which, given a simple comment, should be clear to anyone.

    --
    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.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Thanks a lot matsp. I will try it right now.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    It works fine, but I'm having problems with negative numbers. The function doesn't recognize whether the number is greater or less than zero.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you mean, can you give an example, and perhaps post the code you are using for the "number detection". Negative numbers should be fine with strtol.

    --
    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.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Nevermind, now it's working just fine. Thanks a lot for your help.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Location
    PUNE
    Posts
    23
    Quote Originally Posted by mike_g View Post
    You could use strtol to find out. If it returns zero check if the string is 0, if not then its not a number.
    hp so u gOt iT mIkeY

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 09:43 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

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