20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

480 Appendix B The Standard C Library<br />

In the descriptions that follow, s is a po<strong>in</strong>ter to a null-term<strong>in</strong>ated str<strong>in</strong>g, end is a po<strong>in</strong>ter<br />

to a character po<strong>in</strong>ter, and base is an <strong>in</strong>t.<br />

All rout<strong>in</strong>es skip lead<strong>in</strong>g whitespace characters <strong>in</strong> the str<strong>in</strong>g and stop their scan upon<br />

encounter<strong>in</strong>g a character that is <strong>in</strong>valid for the type of value be<strong>in</strong>g converted.<br />

double atof (s)<br />

Converts the str<strong>in</strong>g po<strong>in</strong>ted to by s <strong>in</strong>to a float<strong>in</strong>g-po<strong>in</strong>t number, return<strong>in</strong>g the result.<br />

<strong>in</strong>t atoi (s)<br />

Converts the str<strong>in</strong>g po<strong>in</strong>ted to by s <strong>in</strong>to an <strong>in</strong>t,return<strong>in</strong>g the result.<br />

<strong>in</strong>t atol (s)<br />

Converts the str<strong>in</strong>g po<strong>in</strong>ted to by s <strong>in</strong>to a long <strong>in</strong>t,return<strong>in</strong>g the result.<br />

<strong>in</strong>t atoll (s)<br />

Converts the str<strong>in</strong>g po<strong>in</strong>ted to by s <strong>in</strong>to a long long <strong>in</strong>t,return<strong>in</strong>g the result.<br />

double strtod (s, end)<br />

Converts s to double,return<strong>in</strong>g the result. A po<strong>in</strong>ter to the character that term<strong>in</strong>ated<br />

the scan is stored <strong>in</strong>side the character po<strong>in</strong>ter po<strong>in</strong>ted to by end,provided end is not a<br />

null po<strong>in</strong>ter.<br />

As an example, the code<br />

#<strong>in</strong>clude <br />

...<br />

char buffer[] = " 123.456xyz", *end;<br />

double value;<br />

...<br />

value = strtod (buffer, &end);<br />

has the effect of stor<strong>in</strong>g the value 123.456 <strong>in</strong>side value.The character po<strong>in</strong>ter variable<br />

end is set by strtod to po<strong>in</strong>t to the character <strong>in</strong> buffer that term<strong>in</strong>ated the<br />

scan. In this case, it is set po<strong>in</strong>t<strong>in</strong>g to the character 'x'.<br />

float strtof (s, end)<br />

Is like strtod, except converts its argument to float.<br />

long <strong>in</strong>t strtol (s, end, base)<br />

Converts s to long <strong>in</strong>t,return<strong>in</strong>g the result. base is an <strong>in</strong>teger base number between<br />

2 and 36, <strong>in</strong>clusive.The <strong>in</strong>teger is <strong>in</strong>terpreted accord<strong>in</strong>g to the specified base. If base<br />

is 0, the <strong>in</strong>teger can be expressed <strong>in</strong> either base 10, octal (lead<strong>in</strong>g 0), or hexadecimal<br />

(lead<strong>in</strong>g 0x or 0X). If base is 16, the value can optionally be preceded by a lead<strong>in</strong>g 0x<br />

or 0X.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!