Unix, C, and C++
Function Reference
Numeric Conversions




  int atol(char * s)
      include: <stdlib.h>
      Converts a string representing a decimal integer into an int.
      The initial portion of the string consisting only of decimal digits is converted
      into the int that it represents. As soon as a character that can not be part of
      a decimal integer is encountered, the conversion is finished. No errors are
      possible.
      refer also to strtol, which is generally better.



  short int atoi(char * s)
      include: <stdlib.h>
      Do not use this function!
      atoi is the same as atol, except that it only works for 16-bit ints.



  double atof(char * s)
      include: <stdlib.h>
      Converts a string representing a floating point number into a double.
      The initial portion of the string consisting of a valid number is converted
        into the int that it represents. As soon as a character that can not be part of
        a decimal floating-point number is encountered, the conversion is finished. 
      No errors are possible.
      Refer also to strtod, which is generally better.



  int strtol(char * s, char * * end, int base)
      include: <stdlib.h>
      Converts a string representing an integer into an int.
      The second parameter must be the address of a char * variable.
      The third parameter is the base to be used in the conversion, must be between
         2 and 36 (or zero which means "work it out for yourself").
      Characters are taken from the beginning of the first (string) parameter so long
        as they form a valid integer in the given base. As soon as an invalid character
        is reached, the conversion finishes and a pointer to the first invalid character
        is stored in the variable whose address was provided in the second parameter.
        If the entire string made a valid integer, this variable will point to the '\0'.
      If the second parameter is NULL, it is ignored.
      If the base is given as 0, it is determined by context. A string beginning with
        "0x" is taken as base 16, "0" means base 8, and anything else means base 10.
      Example:
        char * s="1234";
        char * end;
        int n = strtol(s, &end, 10);
        if (*end == '\0')
          printf("%s = %d\n", s, n);
        else
          printf("%s is not a valid decimal integer\n", s);



  double strtod(char * s, char * * end)
      include: <stdlib.h>
      Converts a string representing a floating point number into a double.
      The second parameter must be the address of a char * variable.
      Characters are taken from the beginning of the first (string) parameter so long
        as they form a valid number. As soon as an invalid character is reached, the 
        conversion finishes and a pointer to the first invalid character is stored in 
        the variable whose address was provided in the second parameter.
        If the entire string made a valid number, this variable will point to the '\0'.
      If the second parameter is NULL, it is ignored.
      Example:
        char * s="+12.34e-02";
        char * end;
        double x = strtod(s, &end, 10);
        if (*end == '\0')
          printf("%s = %f\n", s, x);
        else
          printf("%s is not a valid number\n", s);


See also:
  sscanf: general conversion from string to anything
  sprintf: general conversion from anything to string