Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>strtol() convert string to long decimal integer</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 strtol()                Convert String to Long Decimal Integer

 #include   <stdlib.h>

 long       strtol(nptr,endptr,base);
 const char *nptr;                       Pointer to string
 char       **endptr;                    Pointer to end of scan
 int        base;                        Number base to use

    strtol() converts a string to a long-integer value.  The string must
    consist of a sequence of characters that can be interpreted as a
    long-integer value.

    The string must match this sequence:

                  [whitespace] [sign] [0] [X] [digits]

    strtol() stops reading the string at the first character that cannot
    be recognized as part of a long-integer value or at the first numeric
    character greater than or equal to 'base'.  If 'endptr' is not NULL,
    it points to the character that stopped the scan.

    The string is interpreted as follows: If 'base' is between 2 and 36,
    then 'base' is the base of the long-integer represented in 'nptr'.
    If 'base' is 0, then the first characters of the string pointed to by
    'nptr' determine the base. If 'base' is 0 and the first character of
    'nptr' is 0 and the second character is '1'-'7', the value is an
    octal integer.  If 'base' is 0 and the first character of 'nptr' is
    0, and the second character is 'x' or 'X', the value is a hexadecimal
    integer.  If 'base' is 0 and the first character of 'nptr' is '1'-
    '9', the value is a decimal integer.

    Returns:    The long value represented by the string.  LONG_MAX or
                LONG_MIN are returned when the representation would cause
                an overflow or underflow.  Zero is returned if conversion
                cannot be performed.  On error, 'errno' is set to ERANGE.

   -------------------------------- Example ---------------------------------

    The following statements convert 'string' to a long integer

           #include <stdlib.h>
           #include <stdio.h>

           int bas2 = 2;
           int bas8 = 8;
           int bas16 = 16;
           char *string = "12050a03";
           long longint;
           char *termn;

           main()
           {
               longint = strtol(string,&termn,bas2);
               printf("%s (base %d) = %ld\n",string,bas2,longint);

               longint = strtol(string,&termn,bas8);
               printf("%s (base %d) = %ld\n",string,bas8,longint);
           }


See Also: strtod() atof() atol()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson