Programming in C

(Barry) #1
480 Appendix B The Standard C Library

In the descriptions that follow,sis a pointer to a null-terminated string,endis a pointer
to a character pointer, and baseis an int.
All routines skip leading whitespace characters in the string and stop their scan upon
encountering a character that is invalid for the type of value being converted.
double atof (s)
Converts the string pointed to by sinto a floating-point number, returning the result.
int atoi (s)
Converts the string pointed to by sinto an int,returning the result.
int atol (s)
Converts the string pointed to by sinto a long int,returning the result.
int atoll (s)
Converts the string pointed to by sinto a long long int,returning the result.
double strtod (s, end)
Converts sto double,returning the result. A pointer to the character that terminated
the scan is stored inside the character pointer pointed to by end,provided endis not a
null pointer.
As an example, the code
#include <stdlib.h>
...
char buffer[] = " 123.456xyz", *end;
double value;
...
value = strtod (buffer, &end);
has the effect of storing the value 123.456inside value.The character pointer vari-
able endis set by strtodto point to the character in bufferthat terminated the
scan. In this case, it is set pointing to the character 'x'.
float strtof (s, end)
Is like strtod, except converts its argument to float.
long int strtol (s, end, base)
Converts sto long int,returning the result.baseis an integer base number between
2 and 36, inclusive.The integer is interpreted according to the specified base. If base
is 0, the integer can be expressed in either base 10, octal (leading 0 ), or hexadecimal
(leading 0xor 0X). If baseis 16, the value can optionally be preceded by a leading 0x
or 0X.

21 0672326663 AppB 6/10/04 2:03 PM Page 480

Free download pdf