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 - <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);
 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 is a
    sequence of characters that can be interpreted as a long-integer
    value.  strtol() stops reading the string at the first character that
    cannot be recognized as part of a long-integer value. This character
    could also be 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 must match this sequence:

                  [whitespace] [sign] [0] [x] [digits]

    If 'base' is between 2 and 36, then 'base' is the base of the
    long-integer represented in 'nptr'.  If 'base' is 0, the first few
    characters of 'nptr' determine the base: If the first character is 0
    and the second character is '1'-'7', the value is an octal integer;
    if the first character is 0 and the second character is 'x' or 'X',
    the value is a hexadecimal integer; if the first character 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.  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);

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

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

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