Thread: strtol

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

    strtol

    i was wondering id neone could educate me as to how to use the strtol function.. I need it to to convert a string to an integer and tell me if the string contains a non digit.. an example would make it much easier for me to undertstand.

    cheers

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        char str[] = "1236 1110011    6a66a";
        char *pstr;
        int num1, num2, num3;
        
        num1 = strtol(str, &pstr, 10);
        num2 = strtol(pstr, &pstr, 2);
        num3 = strtol(pstr, &pstr, 16);
        
        printf("Base 10 - &#37;d\n", num1);
        printf("Base 2 - %d\n", num2);
        printf("Base 16 - %d\n", num3);
        
        getchar();
        return 0;
    }
    
    /* my output
    Base 10 - 1236
    Base 2 - 115
    Base 16 - 435818
    
    */
    ssharish

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    so wat part of this function can i use to tell me if the user didnt just enter an integer??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Compare the start pointer with the end pointer, and if they're the same then the first char wasn't an integer char ('0' to '9').
    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.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    i take it pstr is the end pointer but which is the start pointer?? and wat if the first character was a digit but the second one wasnt?
    Last edited by kiz; 10-21-2007 at 02:22 AM.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    say i enterd 12312 as input... wat does the end pointer get to set to?? wat can i compare it to to validate the input as an integer

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    if ( str == pstr )
    Means the first character of str was not 0 to 9
    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.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    yeh but wat if i entered 123a1231 as input?? how would i be able to tell then?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.*

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    The result would be 123 and the end pointer would be pointing at the 'a'.
    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. explanation of STRTOL function
    By adash in forum C Programming
    Replies: 2
    Last Post: 05-25-2009, 02:54 PM
  2. strtol() / malloc
    By sniperwire in forum C Programming
    Replies: 3
    Last Post: 11-02-2008, 09:01 PM
  3. Parse line using strtol
    By mike_g in forum C++ Programming
    Replies: 3
    Last Post: 10-05-2007, 09:47 AM
  4. strtol() is the only way?
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 06-07-2006, 02:32 PM
  5. strtol with floating point
    By Laserve in forum C Programming
    Replies: 3
    Last Post: 07-26-2004, 06:02 AM